Skip to content

Commit 5d77c6a

Browse files
committed
external: fix submodule handling for parallel builds, submodule URL changes.
If we change an upstream URL, all submodules break. Users would need to run 'git submodule sync'. Note that the libbacktrace fix was merged upstream so this is no longer necessary, but it's good for future changes. Also, stress-testing reveals that git submodule fails locking '.git/config' when run in paralell. It also segfaults and other problems. This is my final attempt to fix submodules; I've wasted far too many days on obscure problems it creates: I've already lost one copy of my repo to apparently unfixable submodule preoblems. The next "fix" will be to simply import the source code so it works properly. Reported-by: @jsarenik Fixes: ElementsProject#1543 Signed-off-by: Rusty Russell <[email protected]>
1 parent 05ec4b7 commit 5d77c6a

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

external/Makefile

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
SUBMODULES = \
2+
external/libsodium \
3+
external/libwally-core \
4+
external/jsmn \
5+
external/libbase58 \
6+
external/libbacktrace
7+
18
LIBSODIUM_HEADERS := external/libsodium/src/libsodium/include/sodium.h
29
LIBWALLY_HEADERS := external/libwally-core/include/wally_bip32.h \
310
external/libwally-core/include/wally_core.h \
@@ -21,20 +28,19 @@ EXTERNAL_INCLUDE_FLAGS := \
2128

2229
EXTERNAL_LDLIBS := -Lexternal $(patsubst lib%.a,-l%,$(notdir $(EXTERNAL_LIBS)))
2330

24-
# Might exist, but need updating. Nuke and rebuild.
25-
submodcheck-%: FORCE
26-
if git submodule status external/$* | grep -q '^[-+]'; then rm -rf external/$*; git submodule update --init external/$*; fi
31+
submodcheck: FORCE
32+
@tools/refresh-submodules.sh $(SUBMODULES)
2733

2834
# We build libsodium, since Ubuntu xenial has one too old.
2935
external/libsodium.a: external/libsodium/src/libsodium/libsodium.la
3036
$(MAKE) -C external/libsodium DESTDIR=$$(pwd)/external install-exec
3137

32-
external/libsodium/src/libsodium/include/sodium.h: submodcheck-libsodium
38+
external/libsodium/src/libsodium/include/sodium.h: submodcheck
3339

3440
external/libsodium/src/libsodium/libsodium.la: external/libsodium/src/libsodium/include/sodium.h
3541
cd external/libsodium && ./autogen.sh && ./configure CC="$(CC)" --enable-static=yes --host="$(MAKE_HOST)" --build="$(BUILD)" --enable-shared=no --enable-tests=no --prefix=/ --libdir=/ && $(MAKE)
3642

37-
$(LIBWALLY_HEADERS) $(LIBSECP_HEADERS): submodcheck-libwally-core
43+
$(LIBWALLY_HEADERS) $(LIBSECP_HEADERS): submodcheck
3844

3945
# libsecp included in libwally.
4046
# Wildcards here are magic. See http://stackoverflow.com/questions/2973445/gnu-makefile-rule-generating-a-few-targets-from-a-single-source-file
@@ -44,7 +50,7 @@ external/libsecp256k1.% external/libwallycore.%: external/libwally-core/src/secp
4450
external/libwally-core/src/libwallycore.% external/libwally-core/src/secp256k1/libsecp256k1.%: $(LIBWALLY_HEADERS) $(LIBSECP_HEADERS)
4551
cd external/libwally-core && ./tools/autogen.sh && ./configure CC="$(CC)" --enable-static=yes --host="$(MAKE_HOST)" --build="$(BUILD)" --enable-module-recovery --enable-shared=no --prefix=/ --libdir=/ && $(MAKE)
4652

47-
external/jsmn/jsmn.h: submodcheck-jsmn
53+
external/jsmn/jsmn.h: submodcheck
4854

4955
# If we tell Make that the above builds both, it runs it twice in
5056
# parallel. So we lie :(
@@ -61,7 +67,7 @@ LIBBASE58_SRC := external/libbase58/base58.c
6167

6268
$(LIBBASE58_SRC): $(LIBBASE58_HEADERS)
6369

64-
$(LIBBASE58_HEADERS): submodcheck-libbase58
70+
$(LIBBASE58_HEADERS): submodcheck
6571

6672
# Can't be inside submodule, as that makes git think it's dirty.
6773
external/base58.o: $(LIBBASE58_SRC) Makefile
@@ -70,7 +76,7 @@ external/base58.o: $(LIBBASE58_SRC) Makefile
7076
external/libbase58.a: external/base58.o
7177
$(AR) rc $@ $<
7278

73-
external/libbacktrace/backtrace.h: submodcheck-libbacktrace
79+
external/libbacktrace/backtrace.h: submodcheck
7480

7581
# Need separate build dir: changes inside submodule make git think it's dirty.
7682
external/libbacktrace.a: external/libbacktrace/backtrace.h

tools/refresh-submodules.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#! /bin/sh
2+
3+
if [ $# = 0 ]; then
4+
echo "Usage: $0 <submoduledir1>..." >&2
5+
exit 1
6+
fi
7+
8+
# git submodule can't run in parallel. Really.
9+
echo $$ > .refresh-submodules.$$
10+
if ! mv -n .refresh-submodules.$$ .refresh-submodules; then
11+
rm -f .refresh-submodules.$$
12+
exit 0
13+
fi
14+
trap "rm -f .refresh-submodules" EXIT
15+
16+
# Be a little careful here, since we do rm -rf!
17+
for m in "$@"; do
18+
if ! grep -q "path = $m\$" .gitmodules; then
19+
echo "$m is not a submodule!" >&2
20+
exit 1
21+
fi
22+
done
23+
24+
# git submodule can segfault. Really.
25+
if [ $(git submodule status "$@" | grep -c '^ ') != $# ]; then
26+
echo Reinitializing submodules "$@"...
27+
git submodule sync "$@"
28+
rm -rf "$@"
29+
git submodule update --init "$@"
30+
fi

0 commit comments

Comments
 (0)