From 4fe6d9b76703b5662f728650bafe3dcdebea7076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C8=98tefan=20Talpalaru?= Date: Mon, 25 May 2020 04:18:37 +0200 Subject: [PATCH] build system refactoring - unify the "build-..." targets - enable a debug build by default, to simplify development - bump vendor/DOtherSide - avoid DOtherSide checks for docs/tests-specific tools like Doxygen - switch to an in-place build for DOtherSide - silence the DOtherSide build when V=0, make it more verbose with V=1 - don't delete checked out submodules in the "clean" target - update build instructions in the README - centralise Nim compiler options in a top-level "config.nims" (except `-d:debug` which needs to be on the command line) --- Makefile | 90 +++++++++++++++++++++-------------- README.md | 75 ++++++++++++++--------------- config.nims | 51 ++++++++++++++++++++ nim.cfg | 3 ++ src/nim_status_client.nim | 4 +- src/nim_status_client.nim.cfg | 9 ---- vendor/DOtherSide | 2 +- 7 files changed, 147 insertions(+), 87 deletions(-) create mode 100644 config.nims create mode 100644 nim.cfg delete mode 100644 src/nim_status_client.nim.cfg diff --git a/Makefile b/Makefile index bc7d2a1c92..3da69152af 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,8 @@ BUILD_SYSTEM_DIR := vendor/nimbus-build-system appimage \ clean \ deps \ + nim_status_client \ + run \ update ifeq ($(NIM_PARAMS),) @@ -35,37 +37,57 @@ GIT_SUBMODULE_UPDATE := git submodule update --init --recursive else # "variables.mk" was included. Business as usual until the end of this file. - -ifeq ($(OS),Windows_NT) # is Windows_NT on XP, 2000, 7, Vista, 10... - detected_OS := Windows -else - detected_OS := $(strip $(shell uname)) -endif - - -DEFAULT_TARGET := None -ifeq ($(detected_OS), Darwin) - DEFAULT_TARGET := build-macos -else - DEFAULT_TARGET := build-linux -endif - -all: $(DEFAULT_TARGET) +all: nim_status_client # must be included after the default target -include $(BUILD_SYSTEM_DIR)/makefiles/targets.mk +ifeq ($(OS),Windows_NT) # is Windows_NT on XP, 2000, 7, Vista, 10... + detected_OS := Windows +else + detected_OS := $(strip $(shell uname)) +endif + +ifeq ($(detected_OS), Darwin) + NIM_PARAMS := $(NIM_PARAMS) -L:"-framework Foundation -framework Security -framework IOKit -framework CoreServices" +endif + +DOTHERSIDE := vendor/DOtherSide/lib/libDOtherSideStatic.a + +# Qt5 dirs (we can't indent with tabs here) +QT5_PCFILEDIR := $(shell pkg-config --variable=pcfiledir Qt5Core 2>/dev/null) +QT5_LIBDIR := $(shell pkg-config --variable=libdir Qt5Core 2>/dev/null) +ifeq ($(QT5_PCFILEDIR),) + ifeq ($(QTDIR),) + $(error Can't find your Qt5 installation. Please run "$(MAKE) QTDIR=/path/to/your/Qt5/installation/prefix ...") + else + ifeq ($(detected_OS), Darwin) + QT5_PCFILEDIR := $(QTDIR)/clang_64/lib/pkgconfig + QT5_LIBDIR := $(QTDIR)/clang_64/lib + # some manually installed Qt5 instances have wrong paths in their *.pc files, so we pass the right one to the linker here + NIM_PARAMS += --passL:"-F$(QT5_LIBDIR)" + else + QT5_PCFILEDIR := $(QTDIR)/gcc_64/lib/pkgconfig + QT5_LIBDIR := $(QTDIR)/gcc_64/lib + NIM_PARAMS += --passL:"-L$(QT5_LIBDIR)" + endif + endif +endif +export QT5_LIBDIR +# order matters here, due to "-Wl,-as-needed" +NIM_PARAMS += --passL:"$(DOTHERSIDE) $(shell PKG_CONFIG_PATH="$(QT5_PCFILEDIR)" pkg-config --libs Qt5Core Qt5Qml Qt5Gui Qt5Quick Qt5QuickControls2 Qt5Widgets)" + +# TODO: control debug/release builds with a Make var +# We need `-d:debug` to get Nim's default stack traces. +NIM_PARAMS += --outdir:./bin -d:debug +# Enable debugging symbols in DOtherSide, in case we need GDB backtraces from it. +CFLAGS += -g +CXXFLAGS += -g + deps: | deps-common update: | update-common -DOTHERSIDE := None -ifeq ($(detected_OS), Darwin) -DOTHERSIDE := vendor/DOtherSide/build/lib/libDOtherSide.dylib -else -DOTHERSIDE := vendor/DOtherSide/build/lib/libDOtherSide.so -endif - APPIMAGETOOL := appimagetool-x86_64.AppImage $(APPIMAGETOOL): @@ -75,28 +97,23 @@ $(APPIMAGETOOL): $(DOTHERSIDE): | deps echo -e $(BUILD_MSG) "DOtherSide" + cd vendor/DOtherSide && \ - mkdir -p build && \ - cd build && \ - cmake -DCMAKE_BUILD_TYPE=Release .. $(HANDLE_OUTPUT) && \ - $(MAKE) DOtherSide # IF WE WANT TO USE LIBDOTHERSIDE AS STATIC LIBRARY, USE `$(MAKE) DOtherSideStatic` INSTEAD + rm -f CMakeCache.txt && \ + cmake -DCMAKE_BUILD_TYPE=Release -DENABLE_DOCS=OFF -DENABLE_TESTS=OFF -DENABLE_DYNAMIC_LIBS=OFF -DENABLE_STATIC_LIBS=ON . $(HANDLE_OUTPUT) && \ + $(MAKE) VERBOSE=$(V) $(HANDLE_OUTPUT) STATUSGO := vendor/status-go/build/bin/libstatus.a $(STATUSGO): | deps echo -e $(BUILD_MSG) "status-go" + cd vendor/status-go && \ - $(MAKE) statusgo-library + $(MAKE) statusgo-library $(HANDLE_OUTPUT) -build-linux: $(DOTHERSIDE) $(STATUSGO) src/nim_status_client.nim | deps +nim_status_client: | $(DOTHERSIDE) $(STATUSGO) deps echo -e $(BUILD_MSG) "$@" && \ - $(ENV_SCRIPT) nim c -d:nimDebugDlOpen -L:$(STATUSGO) -d:ssl -L:-lm $(NIM_PARAMS) -L:$(DOTHERSIDE) --outdir:./bin src/nim_status_client.nim - -build-macos: $(DOTHERSIDE) $(STATUSGO) src/nim_status_client.nim | deps - echo -e $(BUILD_MSG) "$@" && \ - $(ENV_SCRIPT) nim c -d:nimDebugDlOpen -L:$(STATUSGO) -d:ssl -L:-lm -L:"-framework Foundation -framework Security -framework IOKit -framework CoreServices" $(NIM_PARAMS) -L:$(DOTHERSIDE) --outdir:./bin src/nim_status_client.nim + $(ENV_SCRIPT) nim c $(NIM_PARAMS) --passL:"$(STATUSGO)" --passL:"-lm" src/nim_status_client.nim run: - LD_LIBRARY_PATH=vendor/DOtherSide/build/lib ./bin/nim_status_client + LD_LIBRARY_PATH="$(QT5_LIBDIR)" ./bin/nim_status_client APPIMAGE := NimStatusClient-x86_64.AppImage @@ -135,6 +152,7 @@ $(APPIMAGE): $(DEFAULT_TARGET) $(APPIMAGETOOL) nim-status.desktop appimage: $(APPIMAGE) clean: | clean-common - rm -rf $(APPIMAGE) bin/* vendor/* tmp/dist + rm -rf $(APPIMAGE) bin/* tmp/dist $(STATUSGO) + + $(MAKE) -C vendor/DOtherSide --no-print-directory clean endif # "variables.mk" was not included diff --git a/README.md b/README.md index bc3da1cbbd..5066f29929 100644 --- a/README.md +++ b/README.md @@ -8,34 +8,15 @@ Experiments calling status-go from nim, inspired in [nim-stratus](https://github * QT -install QT https://www.qt.io/download-qt-installer -and add it to the PATH -``` -# Linux -export PATH=$PATH:/path/to/Qt/5.14.2/gcc_64/bin - -# macos -export PATH=$PATH:/path/to/Qt/5.14.2/clang_64/bin -``` - -Linux users can also install Qt through the system's package manager: +Linux users should install Qt through the system's package manager: ``` # Debian/Ubuntu: sudo apt install qtbase5-dev qtdeclarative5-dev qml-module-qt-labs-platform ``` -* go - (used to build status-go) - -``` -# linux - - -# macos -brew install go -``` - -### 1. Install QT, and add it to the PATH +If that's not possible, manually install QT from https://www.qt.io/download-qt-installer +and add it to the PATH ``` # Linux @@ -45,24 +26,43 @@ export PATH=$PATH:/path/to/Qt/5.14.2/gcc_64/bin export PATH=$PATH:/path/to/Qt/5.14.2/clang_64/bin ``` +* Go - (used to build status-go) + +``` +# Linux + + +# macOS +brew install go +``` + +### 1. Install QT, and add it to the PATH + +``` +# Linux users should use their distro's package manager, but in case they do a manual install: +export PATH=$PATH:/path/to/Qt/5.14.2/gcc_64/bin + +# macOS: +export PATH=$PATH:/path/to/Qt/5.14.2/clang_64/bin +``` + ### 2. Clone the repo and build `nim-status-client` ``` -git clone https://github.com/status-im/nim-status-client/ --recurse-submodules +git clone https://github.com/status-im/nim-status-client +cd nim-status-client +make update make ``` -if you previously cloned the repo without the `--recurse-submodule` options, then do +For more output use `make V=1 ...`. -``` -git submodule update --init --recursive -make -``` +Use 4 CPU cores with `make -j4 ...`. -for more output use `make V=1` +Users with manually installed Qt5 packages need to run `make QTDIR="/path/to/Qt" ...` -**Trouble Shooting**: +**Troubleshooting**: -If the `make` command fails due to already installed homebrew packages, such as: +If the `make` command fails due to already installed Homebrew packages, such as: ``` Error: protobuf 3.11.4 is already installed @@ -74,15 +74,12 @@ make: *** [vendor/status-go/build/bin/libstatus.a] Error 2 This can be fixed by uninstalling the package e.g. `brew uninstall protobuf` followed by rerunning `make`. -### 3. Setup Library Path -``` -export LD_LIBRARY_PATH=vendor/DOtherSide/build/lib/ -``` - -### 4. Run the app +### 3. Run the app ``` -./bin/nim_status_client +make run +# or +LD_LIBRARY_PATH=vendor/DOtherSide/lib ./bin/nim_status_client ``` ## Development @@ -92,7 +89,7 @@ If making changes in the nim code `src/` then doing `make` again is needed (it's ## Cold Reload -### 5. "Cold" reload using VSCode +### "Cold" reload using VSCode We can setup a "cold" reload, whereby the app will be rebuilt and restarted when changes in the source are saved. This will not save state, as the app will be restarted, but it will save us some time from manually restarting the app. We can handily force an app rebuild/relaunch with the shortcut `Cmd+Shift+b` (execute the default build task, which we'll setup below). diff --git a/config.nims b/config.nims new file mode 100644 index 0000000000..95746652e6 --- /dev/null +++ b/config.nims @@ -0,0 +1,51 @@ +when defined(macosx): + import algorithm, strutils + +if defined(release): + switch("nimcache", "nimcache/release/$projectName") +else: + switch("nimcache", "nimcache/debug/$projectName") + +proc linkLib(name: string): string = + var resLib = name + + when defined(macosx): + # In macOS Catalina, unversioned libraries may be broken stubs, so we need to + # find a versioned one: https://github.com/status-im/nim-status-client/pull/209 + var matches: seq[string] + for path in listFiles("/usr/lib"): + # /usr/lib/libcrypto.0.9.8.dylib + let file = path[9..^1] + # libcrypto.0.9.8.dylib + if file.startsWith("lib" & name) and file != "lib" & name & ".dylib": + matches.add(path) + matches.sort(order = SortOrder.Descending) + if matches.len > 0: + resLib = matches[0] + # Passing "/usr/lib/libcrypto.44.dylib" directly to the linker works for + # dynamic linking. + return resLib + + return "-l" & resLib + +--threads:on +--opt:speed # -O3 +--debugger:native # passes "-g" to the C compiler +--dynliboverrideall # don't use dlopen() +--define:ssl # needed by the stdlib to enable SSL procedures + +if defined(macosx): + --tlsEmulation:off + # DYLD_LIBRARY_PATH doesn't seem to work with Qt5 + switch("passL", "-rpath" & " " & getEnv("QT5_LIBDIR")) + switch("passL", "-lstdc++") + # dynamically link these libs, since we're opting out of dlopen() + switch("passL", linkLib("crypto")) + switch("passL", linkLib("ssl")) + # https://code.videolan.org/videolan/VLCKit/-/issues/232 + switch("passL", "-Wl,-no_compact_unwind") +else: + switch("passL", linkLib("crypto") & " " & linkLib("ssl")) # dynamically link these libs, since we're opting out of dlopen() + switch("passL", "-Wl,-as-needed") # don't link libraries we're not actually using + +--define:chronicles_line_numbers # useful when debugging diff --git a/nim.cfg b/nim.cfg new file mode 100644 index 0000000000..c16a8a07be --- /dev/null +++ b/nim.cfg @@ -0,0 +1,3 @@ +# we need to link C++ libraries +gcc.linkerexe="g++" + diff --git a/src/nim_status_client.nim b/src/nim_status_client.nim index 40d9983f8f..ba77d8dc79 100644 --- a/src/nim_status_client.nim +++ b/src/nim_status_client.nim @@ -65,7 +65,7 @@ proc mainProc() = login.init(nodeAccounts) engine.setRootContextProperty("loginModel", login.variant) - + onboarding.init() engine.setRootContextProperty("onboardingModel", onboarding.variant) @@ -76,7 +76,7 @@ proc mainProc() = signalController.addSubscriber(SignalType.DiscoverySummary, chat) signalController.addSubscriber(SignalType.NodeLogin, login) signalController.addSubscriber(SignalType.NodeLogin, onboarding) - + engine.setRootContextProperty("signals", signalController.variant) engine.load("../ui/main.qml") diff --git a/src/nim_status_client.nim.cfg b/src/nim_status_client.nim.cfg deleted file mode 100644 index fbbf66627e..0000000000 --- a/src/nim_status_client.nim.cfg +++ /dev/null @@ -1,9 +0,0 @@ ---threads:on ---tlsEmulation:off - -@if release: - nimcache = "nimcache/release/$projectName" -@else: - nimcache = "nimcache/debug/$projectName" -@end - diff --git a/vendor/DOtherSide b/vendor/DOtherSide index 3ecf2595f1..407787b72b 160000 --- a/vendor/DOtherSide +++ b/vendor/DOtherSide @@ -1 +1 @@ -Subproject commit 3ecf2595f1616b36b5de95227f8a7b5581d419c3 +Subproject commit 407787b72bb0e861c98a26a7e7dc768911830f30