Makefile 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # Copyright (C) 2018-2025 Ruilin Peng (Nick) <[email protected]>.
  2. #
  3. # smartdns is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # smartdns is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. BIN=smartdns-ui
  16. PREFIX := /usr
  17. SBINDIR := $(PREFIX)/sbin
  18. SLIBDIR := $(PREFIX)/lib
  19. DESTDIR :=
  20. CARGO_BUILD_ENV :=
  21. SMARTDNS_SRC_DIR=../../src
  22. ifeq ($(origin CC), environment)
  23. ifeq ($(CARGO_BUILD_TARGET),)
  24. ifeq ($(CARGO_BUILD_ARGS),)
  25. # find default target by compiler
  26. ARCH_TARGET:=$(shell $(CC) -dumpmachine)
  27. override CARGO_BUILD_TARGET:=$(shell rustc --print target-list | grep $(ARCH_TARGET) | head -n 1)
  28. ifeq ($(CARGO_BUILD_TARGET),)
  29. # not found, try to find target by compiler
  30. ARCH_TARGET:=$(shell $(CC) -dumpmachine | sed 's/-[^-]*-linux-/-linux-/')
  31. ARCH=$(shell echo $(ARCH_TARGET) | cut -d - -f 1)
  32. ABI=$(shell echo $(ARCH_TARGET) | cut -d - -f 3)
  33. ifneq ($(ABI),)
  34. # force set target to $(ARCH)-unknown-linux-$(ABI)
  35. override CARGO_BUILD_TARGET:=$(shell rustc --print target-list | grep $(ARCH) | grep linux | grep $(ABI) | grep unknown | head -n 1)
  36. endif
  37. endif
  38. ifneq ($(CARGO_BUILD_TARGET),)
  39. ARCH_TARGET_PATH=$(CARGO_BUILD_TARGET)/
  40. override CARGO_RUSTFLAGS=-C linker=$(CC)
  41. CARGO_BUILD_ARGS +=--target=$(CARGO_BUILD_TARGET)
  42. endif
  43. endif
  44. endif
  45. IS_NATIVE_BUILD=$(shell [ "$(CC)" = "cc" ] || [ "$(CC)" = "gcc" ] && echo 1 || echo 0)
  46. ifeq ($(IS_NATIVE_BUILD), 0)
  47. # find sysroot
  48. SYSROOT:=$(shell $(CC) -print-sysroot)
  49. ifeq ($(SYSROOT),)
  50. # if sysroot is not set, try find sysroot from compiler default include path
  51. SYSROOT:=$(shell $(CC) -xc /dev/null -E -Wp,-v 2>&1 | sed -n 's,^ ,,p' | head -n 1)
  52. ifneq ($(SYSROOT),)
  53. # find sysroot, add sysroot to BINDGEN_EXTRA_CLANG_ARGS
  54. SYSROOT:=$(SYSROOT)/..
  55. override CARGO_BUILD_ENV += BINDGEN_EXTRA_CLANG_ARGS="--sysroot=$(SYSROOT)"
  56. CARGO_BUILD_ARGS +=--target=$(CARGO_BUILD_TARGET)
  57. endif
  58. endif
  59. endif
  60. endif
  61. ifneq ($(CARGO_BUILD_TARGET),)
  62. ARCH_TARGET_PATH=$(CARGO_BUILD_TARGET)/
  63. endif
  64. # check and install bindgen
  65. IS_BINDGEN_INSTALL=$(shell which bindgen 2>/dev/null)
  66. ifeq ($(IS_BINDGEN_INSTALL),)
  67. $(shell cargo install --force --locked bindgen-cli)
  68. endif
  69. ifneq ($(CARGO_BUILD_TARGET),)
  70. CARGO_BUILD_TARGET_INSTALL=$(shell rustup target list | grep $(CARGO_BUILD_TARGET) | grep installed)
  71. ifeq ($(CARGO_BUILD_TARGET_INSTALL),)
  72. # install target
  73. $(shell rustup target add $(CARGO_BUILD_TARGET))
  74. endif
  75. endif
  76. IS_MUSL=$(shell $(CC) -v 2>&1 | grep -i musl >/dev/null 2>&1 && echo 1 || echo 0)
  77. ifeq ($(IS_MUSL), 1)
  78. override CARGO_RUSTFLAGS +=-C target-feature=-crt-static
  79. endif
  80. ifdef DEBUG
  81. CARGO_BUILD_PATH=target/$(ARCH_TARGET_PATH)debug
  82. SMARTDNS_BUILD_TYPE=DEBUG=1
  83. else
  84. ifdef OPTIMIZE_SIZE
  85. CARGO_BUILD_ARGS += --profile release-optmize-size
  86. CARGO_BUILD_PATH=target/$(ARCH_TARGET_PATH)release-optmize-size
  87. else
  88. CARGO_BUILD_ARGS +=--release
  89. CARGO_BUILD_PATH=target/$(ARCH_TARGET_PATH)release
  90. endif
  91. SMARTDNS_BUILD_TYPE=
  92. endif
  93. .PHONY: all clean install $(BIN)
  94. all: $(BIN)
  95. test-prepare:
  96. $(MAKE) -C $(SMARTDNS_SRC_DIR) libsmartdns-test.a
  97. $(BIN):
  98. CXXFLAGS= CFLAGS= MAKEFLAGS= RUSTFLAGS="$(CARGO_RUSTFLAGS) $(RUSTFLAGS)" $(CARGO_BUILD_ENV) cargo build $(CARGO_BUILD_ARGS) --features "build-release"
  99. cp $(CARGO_BUILD_PATH)/libsmartdns_ui.so target/smartdns_ui.so
  100. install: $(BIN)
  101. install -v -m 0644 -D -t $(DESTDIR)$(SLIBDIR)/smartdns target/smartdns_ui.so
  102. test: test-prepare
  103. MAKEFLAGS= cargo test
  104. clean:
  105. cargo clean
  106. $(MAKE) -C $(SMARTDNS_SRC_DIR) clean
  107. rm -rf target/smartdns_ui.so