From d7984e5f221ccbe8624d18790920038781720bc6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Wed, 10 Aug 2016 12:17:31 +0200
Subject: Support 'make -j' when compiling ASN.1 modules

When attempting to build multiple ASN.1 modules in parallel (e.g. by
running 'make -j'), the ASN.1 compiler could crash because the names
of the .asn1db files clashed. For example, if A.asn1 and B.asn1 both
import from C.asn1, the compiler would write a C.asn1db file when
compiling A.asn1 and when compiling B.asn1.

We can avoid this problem if the compiler only writes the
module's own .asn1db file. That is, when compiling A.asn1,
the compiler would only write A.asn1db, not C.asn1db. Also,
make sure that we make the write atomic by first writing to
a temporary file that is then renamed.
---
 lib/asn1/src/asn1_db.erl | 4 +++-
 lib/asn1/src/asn1ct.erl  | 3 +--
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/lib/asn1/src/asn1_db.erl b/lib/asn1/src/asn1_db.erl
index 557eca0ffd..869ea310aa 100644
--- a/lib/asn1/src/asn1_db.erl
+++ b/lib/asn1/src/asn1_db.erl
@@ -106,7 +106,9 @@ loop(#state{parent = Parent, monitor = MRef, table = Table,
             loop(State);
         {save, OutFile, Mod} ->
             [{_,Mtab}] = ets:lookup(Table, Mod),
-            ok = ets:tab2file(Mtab, OutFile),
+	    TempFile = OutFile ++ ".#temp",
+            ok = ets:tab2file(Mtab, TempFile),
+	    ok = file:rename(TempFile, OutFile),
             loop(State);
         {From, {new, Mod, Erule}} ->
             [] = ets:lookup(Table, Mod),	%Assertion.
diff --git a/lib/asn1/src/asn1ct.erl b/lib/asn1/src/asn1ct.erl
index dd269f095d..8783b5418d 100644
--- a/lib/asn1/src/asn1ct.erl
+++ b/lib/asn1/src/asn1ct.erl
@@ -221,9 +221,8 @@ check_pass(#st{code=M,file=File,includes=Includes,
 	    {error,St#st{error=Reason}}
     end.
 
-save_pass(#st{code=M,erule=Erule,dbfile=DbFile}=St) ->
+save_pass(#st{code=M,erule=Erule}=St) ->
     ok = asn1ct_check:storeindb(#state{erule=Erule}, M),
-    asn1_db:dbsave(DbFile,M#module.name),
     {ok,St}.
 
 parse_listing(#st{code=Code,outfile=OutFile0}=St) ->
-- 
cgit v1.2.3