Skip to content

Commit

Permalink
feat: auto-specify build type for building external packages (#159)
Browse files Browse the repository at this point in the history
* feat: auto-define `CMAKE_BUILD_TYPE` in `cdeps_build_package` function

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

* test: specify minimum required CMake version in some test scripts

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

---------

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>
  • Loading branch information
threeal authored Oct 10, 2024
1 parent b329a1b commit f467be9
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ if(CDEPS_ENABLE_TESTS)
find_package(Assertion 1.0.0 REQUIRED)
assertion_add_test(test/test_build_generator.cmake)
assertion_add_test(test/test_build_options.cmake)
assertion_add_test(test/test_build_type.cmake)
assertion_add_test(test/test_build.cmake)
assertion_add_test(test/test_download_submodule.cmake)
assertion_add_test(test/test_download_version.cmake)
Expand Down
16 changes: 15 additions & 1 deletion cmake/CDeps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,16 @@ endfunction()
# same build system generator as the main project, specified by the
# `CMAKE_GENERATOR` variable.
#
# If the `OPTIONS` option is specified, an additional variable specified in each
# If the `OPTIONS` option is specified, additional variables specified in each
# `<options>...` will be defined for building the package. The `<options>...`
# must be in the format `NAME=VALUE`, where `NAME` is the variable name and
# `VALUE` is the variable value.
#
# If the `CMAKE_BUILD_TYPE` variable is defined in the main project but not
# specified in `<options>...`, it will append that variable to `<options>...`,
# making the package be built using the same build type as the main project by
# default.
#
# This function outputs the `<name>_BUILD_DIR` variable, which contains the path
# to the built external package.
function(cdeps_build_package NAME)
Expand All @@ -149,6 +154,15 @@ function(cdeps_build_package NAME)
set(ARG_GENERATOR "${CMAKE_GENERATOR}")
endif()

foreach(OPTION IN LISTS ARG_OPTIONS)
string(REGEX REPLACE "=.*" "" OPTION "${OPTION}")
list(APPEND OPTIONS_NAMES "${OPTION}")
endforeach()

if(DEFINED CMAKE_BUILD_TYPE AND NOT "CMAKE_BUILD_TYPE" IN_LIST OPTIONS_NAMES)
list(APPEND ARG_OPTIONS CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE})
endif()

cdeps_get_package_dir("${NAME}" PACKAGE_DIR)
if(NOT EXISTS ${PACKAGE_DIR}/src.lock)
message(FATAL_ERROR "CDeps: ${NAME} must be downloaded before building")
Expand Down
2 changes: 2 additions & 0 deletions test/test_build.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cmake_minimum_required(VERSION 3.21)

include(${CMAKE_CURRENT_LIST_DIR}/../cmake/CDeps.cmake)

set(CMAKE_GENERATOR "Unix Makefiles")
Expand Down
2 changes: 2 additions & 0 deletions test/test_build_generator.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cmake_minimum_required(VERSION 3.21)

include(${CMAKE_CURRENT_LIST_DIR}/../cmake/CDeps.cmake)

set(CDEPS_ROOT .cdeps)
Expand Down
2 changes: 2 additions & 0 deletions test/test_build_options.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cmake_minimum_required(VERSION 3.21)

include(${CMAKE_CURRENT_LIST_DIR}/../cmake/CDeps.cmake)

set(CDEPS_ROOT .cdeps)
Expand Down
58 changes: 58 additions & 0 deletions test/test_build_type.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
cmake_minimum_required(VERSION 3.21)

include(${CMAKE_CURRENT_LIST_DIR}/../cmake/CDeps.cmake)

set(CDEPS_ROOT .cdeps)
file(REMOVE_RECURSE .cdeps)

file(WRITE .cdeps/pkg/src/CMakeLists.txt
"cmake_minimum_required(VERSION 3.5)\n"
"project(pkg LANGUAGES NONE)\n"
"set(BUILD_TYPE \"\${CMAKE_BUILD_TYPE}\" CACHE STRING \"\" FORCE)\n")

file(WRITE .cdeps/pkg/src.lock "pkg github.com/user/pkg main")

section("it should build a package without a build type specified")
cdeps_build_package(pkg)

section("it should generate the lock file")
file(READ .cdeps/pkg/build.lock CONTENT)
assert(CONTENT MATCHES [^OPTIONS])
endsection()
endsection()

section("it should rebuild the package with the main project build type")
block()
set(CMAKE_BUILD_TYPE Release)
cdeps_build_package(pkg)
endblock()

section("it should regenerate the lock file")
file(READ .cdeps/pkg/build.lock CONTENT)
assert(CONTENT MATCHES "OPTIONS CMAKE_BUILD_TYPE=Release$")
endsection()

section("it should rebuild with the correct build type")
assert_execute_process(
COMMAND ${CMAKE_COMMAND} -L -N .cdeps/pkg/build
OUTPUT "BUILD_TYPE:STRING=Release")
endsection()
endsection()

section("it should rebuild the package with the specified build type")
block()
set(CMAKE_BUILD_TYPE Release)
cdeps_build_package(pkg OPTIONS CMAKE_BUILD_TYPE=RelWithDebInfo)
endblock()

section("it should regenerate the lock file")
file(READ .cdeps/pkg/build.lock CONTENT)
assert(CONTENT MATCHES "OPTIONS CMAKE_BUILD_TYPE=RelWithDebInfo$")
endsection()

section("it should rebuild with the correct build type")
assert_execute_process(
COMMAND ${CMAKE_COMMAND} -L -N .cdeps/pkg/build
OUTPUT "BUILD_TYPE:STRING=RelWithDebInfo")
endsection()
endsection()
2 changes: 2 additions & 0 deletions test/test_install.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cmake_minimum_required(VERSION 3.21)

include(${CMAKE_CURRENT_LIST_DIR}/../cmake/CDeps.cmake)

set(CMAKE_GENERATOR "Unix Makefiles")
Expand Down

0 comments on commit f467be9

Please sign in to comment.