Skip to content

Commit

Permalink
Merge pull request #152 from nqminds/cleanup-cmake
Browse files Browse the repository at this point in the history
Various CMake cleanups
  • Loading branch information
aloisklink committed Jun 10, 2022
2 parents 090b2ca + 8bc918a commit 4f36b10
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 63 deletions.
39 changes: 13 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,45 +40,32 @@ set(
)

option(BUILD_ONLY_DOCS "Build only docs" OFF)
option(BUILD_MDNS_SERVICE "Build mDNS service" ON)
option(BUILD_CAPTURE_SERVICE "Build Capture service" ON)
cmake_dependent_option(BUILD_PCAP_LIB "Build pcap library" ON BUILD_CAPTURE_SERVICE OFF)
option(BUILD_HOSTAPD "Build hostapd" OFF)
option(BUILD_SQLITE_LIB "Build sqlite3 library" ON)
option(BUILD_OPENSSL_LIB "Build OpenSSL" ON)
option(BUILD_CMOCKA_LIB "Build cmocka library" OFF)
# off by default since we should apt install libmnl-dev
option(BUILD_MNL_LIB "Build mnl library" OFF)
option(BUILD_NETLINK_LIB "Build netlink library" OFF)
option(BUILD_NL_LIB "Build nl library" OFF)
option(BUILD_UUID_LIB "Build uuid library" ON)

option(BUILD_CAPTURE_SERVICE "Build Capture service" ON)
cmake_dependent_option(BUILD_PCAP_LIB "Build pcap library" ON BUILD_CAPTURE_SERVICE OFF)
cmake_dependent_option(BUILD_MDNS_SERVICE "Build mDNS service" ON BUILD_CAPTURE_SERVICE OFF)

option(USE_NETLINK_SERVICE "Use netlink service" OFF)
cmake_dependent_option(BUILD_MNL_LIB "Build mnl library" ON USE_NETLINK_SERVICE OFF)
cmake_dependent_option(BUILD_NETLINK_LIB "Build netlink library" ON USE_NETLINK_SERVICE OFF)
option(USE_UCI_SERVICE "Use OpenWRT uci service" ON)
option(USE_GENERIC_IP_SERVICE "Use generic ip service" OFF)
option(USE_CRYPTO_SERVICE "Use the crypto service" OFF)
option(USE_SQLITE3_LIB "Use sqlite3 library" ON)
option(USE_UUID_LIB "Use uuid library" ON)
option(USE_ZYMKEY4_HSM "Use the Zymkey4 HSM" OFF)

cmake_dependent_option(BUILD_UCI_LIB "Build OpenWRT UCI library" ON USE_UCI_SERVICE OFF)
option(USE_GENERIC_IP_SERVICE "Use generic ip service" OFF)
if (USE_NETLINK_SERVICE AND USE_UCI_SERVICE AND USE_GENERIC_IP_SERVICE)
message(FATAL_ERROR "Only USE_NETLINK_SERVICE or USE_UCI_SERVICE allowed.")
endif ()

option(USE_CRYPTO_SERVICE "Use the crypto service" OFF)
cmake_dependent_option( BUILD_OPENSSL_LIB "Build OpenSSL" ON USE_CRYPTO_SERVICE OFF)
if (USE_CRYPTO_SERVICE)
add_compile_definitions(TEST_CRYPTO_SERVICE_KEY="12345")
endif ()

if (USE_NETLINK_SERVICE AND USE_UCI_SERVICE AND USE_GENERIC_IP_SERVICE)
message(FATAL_ERROR "Only USE_NETLINK_SERVICE or USE_UCI_SERVICE allowed.")
endif ()

# CMake hides $ENV{MAKEFLAGS}, so we need to define it ourselves
set(LIB_MAKEFLAGS "--jobs=2" CACHE STRING "Flags to pass to make for building libraries (e.g. -j12)")
if(LIB_MAKEFLAGS OR TRUE)
# set makeflags so that OpenSSL can build in parallel
# Prefix with LIB_*, so it only applies when we use
# MAKEFLAGS="$LIB_MAKEFLAGS" in our compile_*.sh scripts
set(ENV{LIB_MAKEFLAGS} "${LIB_MAKEFLAGS}")
endif()
option(USE_ZYMKEY4_HSM "Use the Zymkey4 HSM" OFF)

# Set a default build type if none was specified
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
Expand Down
7 changes: 0 additions & 7 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
"CMAKE_BUILD_TYPE": {
"type": "STRING",
"value": "Debug"
},
"LIB_MAKEFLAGS": {
"type": "STRING",
"value": "-j$(shell nproc)"
}
}
},
Expand All @@ -28,10 +24,8 @@
"displayName": "Linux",
"description": "Default build for Linux (uses netlink)",
"cacheVariables": {
"CROSS_COMPILE_EDGESEC": false,
"BUILD_MNL_LIB": true,
"BUILD_NETLINK_LIB": true,
"BUILD_NL_LIB": true,
"USE_NETLINK_SERVICE": true,
"USE_UCI_SERVICE": false,
"USE_GENERIC_IP_SERVICE": false,
Expand All @@ -49,7 +43,6 @@
"cacheVariables": {
"BUILD_MNL_LIB": false,
"BUILD_NETLINK_LIB": false,
"BUILD_NL_LIB": false,
"USE_NETLINK_SERVICE": false,
"USE_UCI_SERVICE": true,
"USE_GENERIC_IP_SERVICE": false,
Expand Down
16 changes: 1 addition & 15 deletions debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,15 @@
%:
dh $@

# Add --jobs=x/ parallel flag to MAKEFLAGS
# This is used both in the build step,
# but we additionally pass it to cmake configure using
# -DLIB_MAKEFLAGS, so that dependent libs (e.g. OpenSSL)
# are compiled with parallel jobs,
# as cmake configure ignores the MAKEFLAGS variable
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
MAKEFLAGS += --jobs=$(NUMJOBS)
endif

override_dh_auto_configure:
dh_auto_configure -- \
-DCMAKE_LIBRARY_ARCHITECTURE="$(DEB_TARGET_MULTIARCH)" \
-DCMAKE_BUILD_TYPE=Release \
-DCROSS_COMPILE_EDGESEC=OFF \
-DBUILD_MNL_LIB=ON\
-DBUILD_NETLINK_LIB=ON\
-DBUILD_NL_LIB=ON\
-DUSE_NETLINK_SERVICE=ON\
-DUSE_UCI_SERVICE=OFF\
-DBUILD_HOSTAPD=ON\
"-DLIB_MAKEFLAGS=--jobs=$(NUMJOBS)"
-DBUILD_HOSTAPD=ON

# make sure to tell dh_shlibdeps about our private shared libs
override_dh_shlibdeps:
Expand Down
14 changes: 3 additions & 11 deletions docs/docusaurus/docs/compilation.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,10 @@ Configure `cmake` in the `build/` directory by running the following:

```bash
# or for old versions of cmake, do: mkdir build/ && cd build/ && cmake ..
cmake -S . -B build -DLIB_MAKEFLAGS="--jobs=$(nproc)"
cmake -S . -B build
```

The configure stage will download and compile most of the EDGESec dependencies,
so this may take a while.

Setting `-DLIB_MAKEFLAGS="--jobs=$(nproc)"` will mean that while compiling library dependencies,
`make` commands will run using all CPU cores, greatly speeding this building (set a lower number if you have less RAM).

```bash
cmake -S . -B build -DLIB_MAKEFLAGS="--jobs=$(nproc)"
```
The configure stage will download some of the EDGESec dependencies, so this may take a while.

### Building

Expand Down Expand Up @@ -85,7 +77,7 @@ cmake --install build/
Set `-DCMAKE_INSTALL_PREFIX=<YOUR-LOCATION-HERE>` to build for a different location:

```bash
MAKEFLAGS="--jobs=$(nproc)" cmake -B build/ -S . -DCMAKE_INSTALL_PREFIX=/tmp/example-build
cmake -B build/ -S . -DCMAKE_INSTALL_PREFIX=/tmp/example-build
cmake --build build/ --target install -j4
```

Expand Down
9 changes: 8 additions & 1 deletion lib/libnetlink/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ endif()
# libmnl0 linking doesn't work properly, unless we setup RPATHs
SET(CMAKE_INSTALL_RPATH "$ORIGIN" "$<TARGET_LINKER_FILE_DIR:MNL::mnl>")

set(CMAKE_BUILD_TYPE Debug)
# Set a default build type if none was specified
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Debug' as none was specified.")
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
"MinSizeRel" "RelWithDebInfo")
endif()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_GNU_SOURCE")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_LIBMNL")
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ add_subdirectory(capture)


add_library(sqlhook SHARED sqlhook.c)
target_compile_options(sqlhook PRIVATE -fPIC)
target_link_libraries(sqlhook PRIVATE domain os SQLite::SQLite3)

add_library(engine engine.c)
Expand Down
5 changes: 3 additions & 2 deletions src/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_library(log log.c)
target_compile_options(log PRIVATE -fPIC)
set_property(TARGET log PROPERTY POSITION_INDEPENDENT_CODE ON)

add_library(allocs allocs.c)

if (USE_CRYPTO_SERVICE)
Expand All @@ -11,7 +12,7 @@ add_library(base64 base64.c)
target_link_libraries(base64 PRIVATE os log)

add_library(os os.c)
target_compile_options(os PRIVATE -fPIC)
set_property(TARGET os PROPERTY POSITION_INDEPENDENT_CODE ON)
target_link_libraries(os PRIVATE allocs log LibUUID::LibUUID)

add_library(ifaceu ifaceu.c)
Expand Down

0 comments on commit 4f36b10

Please sign in to comment.