Skip to content

Commit

Permalink
rust: Add rust detection to configure and a target to add binaries
Browse files Browse the repository at this point in the history
We detect whether we have the rust tooling available (mainly `cargo`)
and enable or disable the rust libraries, plugins and examples when it
is enabled. Since the rest of the Makefiles assumes that executables
have an associated header and C source file, we also needed to add a
target that we can add non-C binaries to.
  • Loading branch information
cdecker committed Jan 19, 2022
1 parent eababff commit 680fc62
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ doc/lightning*.[1578]
# Ignore unrelated stuff
.DS_Store
.gdb_history

# Rust targets
target
Cargo.lock
16 changes: 15 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ ALL_TEST_PROGRAMS :=
ALL_FUZZ_TARGETS :=
ALL_C_SOURCES :=
ALL_C_HEADERS := header_versions_gen.h version_gen.h
# Extra (non C) targets that should be built by default.
DEFAULT_TARGETS :=

CPPFLAGS += -DBINTOPKGLIBEXECDIR="\"$(shell sh tools/rel.sh $(bindir) $(pkglibexecdir))\""
CFLAGS = $(CPPFLAGS) $(CWARNFLAGS) $(CDEBUGFLAGS) $(COPTFLAGS) -I $(CCANDIR) $(EXTERNAL_INCLUDE_FLAGS) -I . -I/usr/local/include $(SQLITE3_CFLAGS) $(POSTGRES_INCLUDE) $(FEATURES) $(COVFLAGS) $(DEV_CFLAGS) -DSHACHAIN_BITS=48 -DJSMN_PARENT_LINKS $(PIE_CFLAGS) $(COMPAT_CFLAGS) -DBUILD_ELEMENTS=1
Expand Down Expand Up @@ -260,7 +262,7 @@ ifeq ($(HAVE_POSTGRES),1)
LDLIBS += $(POSTGRES_LDLIBS)
endif

default: show-flags all-programs all-test-programs doc-all
default: show-flags all-programs all-test-programs doc-all default-targets

ifneq ($(SUPPRESS_GENERATION),1)
FORCE = FORCE
Expand Down Expand Up @@ -323,6 +325,13 @@ endif
$(call VERBOSE,"printgen $@",tools/generate-wire.py -s -P --page impl $($@_args) ${@:.c=.h} `basename $< .csv | sed 's/_exp_/_/'` < $< > $@ && $(call SHA256STAMP,//,)); \
fi

RUST_PROFILE ?= debug
ifeq ($(RUST_PROFILE),release)
CARGO_OPTS := --release
else
CARGO_OPTS :=
endif

include external/Makefile
include bitcoin/Makefile
include common/Makefile
Expand All @@ -345,6 +354,9 @@ include contrib/libhsmd_python/Makefile
ifneq ($(FUZZING),0)
include tests/fuzz/Makefile
endif
ifneq ($(RUST),0)
# Add Rust Makefiles here
endif

# We make pretty much everything depend on these.
ALL_GEN_HEADERS := $(filter %gen.h,$(ALL_C_HEADERS))
Expand Down Expand Up @@ -604,6 +616,7 @@ update-ccan:
# Now ALL_PROGRAMS is fully populated, we can expand it.
all-programs: $(ALL_PROGRAMS)
all-test-programs: $(ALL_TEST_PROGRAMS) $(ALL_FUZZ_TARGETS)
default-targets: $(DEFAULT_TARGETS)

distclean: clean
$(RM) ccan/config.h config.vars
Expand All @@ -627,6 +640,7 @@ clean: obsclean
find . -name '*gcda' -delete
find . -name '*gcno' -delete
find . -name '*.nccout' -delete
if [ "${RUST}" -eq "1" ]; then cargo clean; fi

# These must both be enabled for update-mocks
ifeq ($(DEVELOPER)$(EXPERIMENTAL_FEATURES),11)
Expand Down
15 changes: 15 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ default_valgrind_setting()
fi
}

default_rust_setting()
{
if cargo --version > /dev/null 2>&1 || [ $? = 0 ]; then
echo 1
else
echo 0
fi
}

set_defaults()
{
# Default values, loaded from environment or canned.
Expand All @@ -129,6 +138,7 @@ set_defaults()
VALGRIND=${VALGRIND:-$(default_valgrind_setting)}
TEST_NETWORK=${TEST_NETWORK:-regtest}
FUZZING=${FUZZING:-0}
RUST=${RUST:-$(default_rust_setting)}
}

usage()
Expand Down Expand Up @@ -167,6 +177,8 @@ usage()
usage_with_default "--enable/disable-ub-sanitizer" "$UBSAN" "enable" "disable"
echo " Compile with undefined behaviour sanitizer"
usage_with_default "--enable/disable-fuzzing" "$FUZZING" "enable" "disable"
echo " Compile with Rust support"
usage_with_default "--enable/disable-rust" "$RUST" "enable" "disable"
exit 1
}

Expand Down Expand Up @@ -222,6 +234,8 @@ for opt in "$@"; do
--disable-ub-sanitize) UBSAN=0;;
--enable-fuzzing) FUZZING=1;;
--disable-fuzzing) FUZZING=0;;
--enable-rust) RUST=1;;
--disable-rust) RUST=0;;
--help|-h) usage;;
*)
echo "Unknown option '$opt'" >&2
Expand Down Expand Up @@ -430,6 +444,7 @@ add_var TEST_NETWORK "$TEST_NETWORK"
add_var HAVE_PYTHON3_MAKO "$HAVE_PYTHON3_MAKO"
add_var SHA256SUM "$SHA256SUM"
add_var FUZZING "$FUZZING"
add_var RUST "$RUST"

# Hack to avoid sha256 name clash with libwally: will be fixed when that
# becomes a standalone shared lib.
Expand Down

0 comments on commit 680fc62

Please sign in to comment.