Makefile 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. @mkdir -p $(CURDIR)/native_contracts_src
  60. @(cd ../../../src && \
  61. tar -cf $(CURDIR)/native_contracts_src/deployooor_contract_src.tar -C contract/deployooor/src --transform 's,^./,,' . && \
  62. tar -cf $(CURDIR)/native_contracts_src/dao_contract_src.tar -C contract/dao/src --transform 's,^./,,' . 2>/dev/null && \
  63. find contract/dao/proof -name '*.zk' -exec tar -rf $(CURDIR)/native_contracts_src/dao_contract_src.tar --transform 's,^.*proof/,proof/,' {} + && \
  64. tar -cf $(CURDIR)/native_contracts_src/money_contract_src.tar -C contract/money/src --transform 's,^./,,' . && \
  65. find contract/money/proof -name '*.zk' -exec tar -rf $(CURDIR)/native_contracts_src/money_contract_src.tar --transform 's,^.*proof/,proof/,' {} + \
  66. )
  67. # Start explorer on darkfid localnet
  68. start-localnet: check-minerd
  69. # Start localnet/testnet/mainnet networks
  70. start-%: check-darkfid check-explorerd
  71. @if [ "$*" != "localnet" ] && [ "$*" != "testnet" ] && [ "$*" != "mainnet" ]; then \
  72. echo "Error: Unsupported network '$*'. Use 'localnet', 'testnet', or 'mainnet'."; \
  73. exit 1; \
  74. fi
  75. @$(MAKE) stop suppress_not_running=1
  76. @echo "Starting explorer node environment $*..."
  77. @sh -c ' \
  78. LOG_DIR=$(LOG_HOME)/$*; \
  79. mkdir -p "$$LOG_DIR"; \
  80. $(if $(filter localnet,$*),$(PROJECT_ROOT)/minerd -c $(PROJECT_ROOT)/bin/minerd/minerd_config.toml & echo $$! >> PIDs.txt; sleep 2;) \
  81. $(PROJECT_ROOT)/darkfid --log "$$LOG_DIR/darkfid.log" -c $(PROJECT_ROOT)/bin/darkfid/darkfid_config.toml --network $* & echo $$! >> PIDs.txt; sleep 2; \
  82. $(call wait_for_darkfid_startup, $$LOG_DIR) \
  83. ./explorerd --log "$$LOG_DIR/explorerd.log" -c ./explorerd_config.toml --network $* & echo $$! >> PIDs.txt; \
  84. $(call wait_for_explorerd_startup, $$LOG_DIR) \
  85. '
  86. # Check and build darkfid if it does not exist
  87. check-darkfid:
  88. @if [ ! -f $(PROJECT_ROOT)/darkfid ]; then \
  89. echo "Building darkfid..."; \
  90. $(MAKE) -C $(PROJECT_ROOT) darkfid; \
  91. fi
  92. # Check and build explorerd if it does not exist
  93. check-explorerd:
  94. @if [ ! -f ./explorerd ]; then \
  95. echo "Building explorerd..."; \
  96. $(MAKE) -C . ; \
  97. fi
  98. # Check and build minerd if it does not exist
  99. check-minerd:
  100. @if [ ! -f $(PROJECT_ROOT)/minerd ]; then \
  101. echo "Building minerd..."; \
  102. $(MAKE) -C $(PROJECT_ROOT) minerd; \
  103. fi
  104. # Stop the running network
  105. # Usage: make stop [suppress_not_running=1]
  106. stop:
  107. @if [ -f PIDs.txt ]; then \
  108. while read PID; do \
  109. kill $$PID 2>/dev/null || echo "Unable to kill PID $$PID"; \
  110. done < PIDs.txt; \
  111. rm -f PIDs.txt; \
  112. sleep 5; \
  113. echo "Stopped explorer node environment"; \
  114. else \
  115. if [ "$(suppress_not_running)" != "1" ]; then \
  116. echo "Explorer node environment not running, nothing to stop."; \
  117. fi; \
  118. fi
  119. # Waits for Darkfid to start
  120. define wait_for_darkfid_startup
  121. log_dir=$(strip $(1)); \
  122. while ! grep -q "Darkfid P2P handler started successfully!" "$$log_dir/darkfid.log" 2>/dev/null; do \
  123. sleep 1; \
  124. done;
  125. endef
  126. # Waits for Explorerd to start
  127. define wait_for_explorerd_startup
  128. log_dir=$(strip $(1)); \
  129. while ! grep -q "Started DarkFi Explorer Node" "$$log_dir/explorerd.log" 2>/dev/null; do \
  130. sleep 1; \
  131. done;
  132. endef
  133. # Waits for network to start
  134. await-startup-%:
  135. @$(call wait_for_darkfid_startup,$(LOG_HOME)/$*)
  136. @$(call wait_for_explorerd_startup,$(LOG_HOME)/$*)
  137. .PHONY: help all clean install uninstall bundle_contracts_src check-minerd check-darkfid check-explorerd stop start-%