Makefile 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. .POSIX:
  2. # Suppress all directory-related messages for cleaner output
  3. MAKEFLAGS += --no-print-directory
  4. # Install prefix
  5. PREFIX = $(HOME)/.cargo
  6. # Cargo binary
  7. CARGO = cargo +nightly
  8. # Compile target
  9. RUST_TARGET = $(shell rustc -Vv | grep '^host: ' | cut -d' ' -f2)
  10. # Uncomment when doing musl static builds
  11. #RUSTFLAGS = -C target-feature=+crt-static -C link-self-contained=yes
  12. # Root directory of the project
  13. PROJECT_ROOT=../../..
  14. # Directory where node logs are stored
  15. LOG_HOME := $(shell echo ~/.local/share/darkfi/logs)
  16. SRC = \
  17. Cargo.toml \
  18. ../../../Cargo.toml \
  19. $(shell find src -type f -name '*.rs') \
  20. $(shell find ../../../src -type f -name '*.rs') \
  21. BIN = $(shell grep '^name = ' Cargo.toml | sed 1q | cut -d' ' -f3 | tr -d '"')
  22. all: $(BIN)
  23. help:
  24. @echo "Explorerd Makefile Commands:"
  25. @echo ""
  26. @echo "Build targets:"
  27. @echo " make - Build the $(BIN) binary"
  28. @echo " make clean - Remove build artifacts"
  29. @echo " make install - Install $(BIN) to $(PREFIX)/bin"
  30. @echo " make uninstall - Remove $(BIN) from $(PREFIX)/bin"
  31. @echo ""
  32. @echo "Network management:"
  33. @echo " make start-localnet - Start the explorer node environment on localnet"
  34. @echo " make start-testnet - Start the explorer node environment on testnet"
  35. @echo " make start-mainnet - Start the explorer node environment on mainnet"
  36. @echo " make stop - Stop all nodes running within the explorer node environment"
  37. @echo ""
  38. @echo "Utility targets:"
  39. @echo " make bundle_contracts_src - Bundle contract sources and ZK proofs into a tar archives in native_contracts_src directory"
  40. @echo " make await-startup-NETWORK - Wait until nodes are ready (used in scripting, replace NETWORK with localnet/testnet/mainnet)"
  41. @echo ""
  42. @echo "Log files are stored in: $(LOG_HOME)/{localnet|testnet|mainnet}/"
  43. $(BIN): $(SRC) bundle_contracts_src
  44. RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) build --target=$(RUST_TARGET) --release --package $@
  45. cp -f ../../../target/$(RUST_TARGET)/release/$@ $@
  46. cp -f ../../../target/$(RUST_TARGET)/release/$@ ../../../$@
  47. clean:
  48. RUSTFLAGS="$(RUSTFLAGS)" $(CARGO) clean --target=$(RUST_TARGET) --release --package $(BIN)
  49. rm -f $(BIN) ../../../$(BIN)
  50. rm -rf native_contracts_src
  51. install: all
  52. mkdir -p $(DESTDIR)$(PREFIX)/bin
  53. cp -f $(BIN) $(DESTDIR)$(PREFIX)/bin
  54. chmod 755 $(DESTDIR)$(PREFIX)/bin/$(BIN)
  55. uninstall:
  56. rm -f $(DESTDIR)$(PREFIX)/bin/$(BIN)
  57. # Bundle native contract sources and their ZK proofs
  58. bundle_contracts_src:
  59. @PROJECT_ROOT=$$(pwd)/$(PROJECT_ROOT); \
  60. CONTRACT_SRC_DIR="$$PROJECT_ROOT/src/contract"; \
  61. CONTRACTS="deployooor money dao"; \
  62. set -e; \
  63. mkdir -p native_contracts_src/tmp; \
  64. trap "rm -rf native_contracts_src/tmp" 0 1 2 15; \
  65. for contract in $$CONTRACTS; do \
  66. mkdir -p native_contracts_src/tmp/$$contract; \
  67. cp -R "$$CONTRACT_SRC_DIR/$$contract/src/"* "native_contracts_src/tmp/$$contract"; \
  68. if [ -d "$$CONTRACT_SRC_DIR/$$contract/proof" ]; then \
  69. mkdir -p native_contracts_src/tmp/$$contract/proof; \
  70. find "$$CONTRACT_SRC_DIR/$$contract/proof" -type f ! -name '*.bin' -exec cp \{\} native_contracts_src/tmp/$$contract/proof/ \; ; \
  71. if find "native_contracts_src/tmp/$$contract/proof" -name '*.json' | grep -q .; then \
  72. mkdir -p "native_contracts_src/tmp/$$contract/proof/witness"; \
  73. mv native_contracts_src/tmp/$$contract/proof/*.json "native_contracts_src/tmp/$$contract/proof/witness"; \
  74. fi; \
  75. fi; \
  76. (cd native_contracts_src/tmp/$$contract && tar --format=pax -cf ../../$${contract}_contract_src.tar *); \
  77. done;
  78. # Start explorer on darkfid localnet
  79. start-localnet: check-minerd
  80. # Start localnet/testnet/mainnet networks
  81. start-%: check-darkfid check-explorerd
  82. @if [ "$*" != "localnet" ] && [ "$*" != "testnet" ] && [ "$*" != "mainnet" ]; then \
  83. echo "Error: Unsupported network '$*'. Use 'localnet', 'testnet', or 'mainnet'."; \
  84. exit 1; \
  85. fi
  86. @$(MAKE) stop suppress_not_running=1
  87. @echo "Starting explorer node environment $*..."
  88. @sh -c ' \
  89. LOG_DIR=$(LOG_HOME)/$*; \
  90. mkdir -p "$$LOG_DIR"; \
  91. $(if $(filter localnet,$*),$(PROJECT_ROOT)/minerd -c $(PROJECT_ROOT)/bin/minerd/minerd_config.toml & echo $$! >> PIDs.txt; sleep 2;) \
  92. $(PROJECT_ROOT)/darkfid --log "$$LOG_DIR/darkfid.log" -c $(PROJECT_ROOT)/bin/darkfid/darkfid_config.toml --network $* & echo $$! >> PIDs.txt; sleep 2; \
  93. $(call wait_for_darkfid_startup, $$LOG_DIR) \
  94. ./explorerd --log "$$LOG_DIR/explorerd.log" -c ./explorerd_config.toml --network $* & echo $$! >> PIDs.txt; \
  95. $(call wait_for_explorerd_startup, $$LOG_DIR) \
  96. '
  97. # Check and build darkfid if it does not exist
  98. check-darkfid:
  99. @if [ ! -f $(PROJECT_ROOT)/darkfid ]; then \
  100. echo "Building darkfid..."; \
  101. $(MAKE) -C $(PROJECT_ROOT) darkfid; \
  102. fi
  103. # Check and build explorerd if it does not exist
  104. check-explorerd:
  105. @if [ ! -f ./explorerd ]; then \
  106. echo "Building explorerd..."; \
  107. $(MAKE) -C . ; \
  108. fi
  109. # Check and build minerd if it does not exist
  110. check-minerd:
  111. @if [ ! -f $(PROJECT_ROOT)/minerd ]; then \
  112. echo "Building minerd..."; \
  113. $(MAKE) -C $(PROJECT_ROOT) minerd; \
  114. fi
  115. # Stop the running network
  116. # Usage: make stop [suppress_not_running=1]
  117. stop:
  118. @if [ -f PIDs.txt ]; then \
  119. while read PID; do \
  120. if ps -p $$PID > /dev/null 2>&1; then \
  121. kill -15 $$PID 2>/dev/null; sleep 5; ps -p $$PID > /dev/null 2>&1 && kill -9 $$PID 2>/dev/null; \
  122. fi; \
  123. done < PIDs.txt; \
  124. rm -f PIDs.txt; \
  125. echo "Stopped explorer node environment"; \
  126. else \
  127. if [ "$(suppress_not_running)" != "1" ]; then \
  128. echo "Explorer node environment not running, nothing to stop."; \
  129. fi; \
  130. fi
  131. # Waits for Darkfid to start
  132. define wait_for_darkfid_startup
  133. log_dir=$(strip $(1)); \
  134. while ! grep -q "Darkfid P2P handler started successfully!" "$$log_dir/darkfid.log" 2>/dev/null; do \
  135. sleep 1; \
  136. done;
  137. endef
  138. # Waits for Explorerd to start
  139. define wait_for_explorerd_startup
  140. log_dir=$(strip $(1)); \
  141. while ! grep -q "Started DarkFi Explorer Node" "$$log_dir/explorerd.log" 2>/dev/null; do \
  142. sleep 1; \
  143. done;
  144. endef
  145. # Waits for network to start
  146. await-startup-%:
  147. @$(call wait_for_darkfid_startup,$(LOG_HOME)/$*)
  148. @$(call wait_for_explorerd_startup,$(LOG_HOME)/$*)
  149. .PHONY: help all clean install uninstall bundle_contracts_src check-minerd check-darkfid check-explorerd stop start-%