From ba97f7b7ca392d74333d8b409f368418ee3a65fa Mon Sep 17 00:00:00 2001 From: Zhuo Zhang Date: Sat, 23 Nov 2024 15:32:08 +0800 Subject: [PATCH] feat: Implement debug postfix handling in CMake and add corresponding tests --- rocbuild.cmake | 24 +++++++++ test.py | 53 ++++++++++++++++++- .../{postfix => debug_postfix}/CMakeLists.txt | 5 +- 3 files changed, 80 insertions(+), 2 deletions(-) rename tests/{postfix => debug_postfix}/CMakeLists.txt (55%) diff --git a/rocbuild.cmake b/rocbuild.cmake index 8203770..8c951e5 100644 --- a/rocbuild.cmake +++ b/rocbuild.cmake @@ -125,5 +125,29 @@ function(rocbuild_copy_opencv_videoio_plugin_dlls target) endfunction() +function(rocbuild_set_debug_postfix TARGET) + # determine TARGET type + get_target_property(TYPE ${TARGET} TYPE) + if(NOT TYPE) + message(FATAL_ERROR "rocbuild_define_package() called with non-existent target: ${TARGET}") + endif() + + # determine if TARGET is imported + get_target_property(IMPORTED ${TARGET} IMPORTED) + if(IMPORTED) + return() + endif() + + # Don't treat for single config generators + if(NOT CMAKE_CONFIGURATION_TYPES) + return() + endif() + + if(TYPE MATCHES "^(STATIC_LIBRARY|SHARED_LIBRARY|EXECUTABLE)$") + set_target_properties(${TARGET} PROPERTIES DEBUG_POSTFIX "_d") + endif() +endfunction() + + rocbuild_set_artifacts_path() rocbuild_enable_ninja_colorful_output() \ No newline at end of file diff --git a/test.py b/test.py index 88f2a4c..cbbfed9 100644 --- a/test.py +++ b/test.py @@ -74,7 +74,7 @@ def test_artifact_path(self): self.assertTrue(os.path.exists('build/artifacts_path/subhello.exe')) shutil.rmtree('build/artifacts_path') - self.check_generate('artifacts_path', args='-G "Visual Studio 17 2022" -A x64') + self.check_generate('artifacts_path') self.check_build('artifacts_path', '--config Release') self.assertTrue(os.path.exists('build/artifacts_path/Release/foo_static.lib')) self.assertTrue(os.path.exists('build/artifacts_path/Release/foo_shared.dll')) @@ -103,6 +103,57 @@ def test_artifact_path(self): shutil.rmtree('build/artifacts_path') + def test_debug_postfix(self): + if os_name == 'windows': + # msbuild + self.check_generate('debug_postfix') + self.check_build('debug_postfix', '--config Debug') + self.assertTrue(os.path.exists('build/debug_postfix/Debug/foo_d.lib')) + self.assertTrue(os.path.exists('build/debug_postfix/Debug/hello_d.exe')) + shutil.rmtree('build/debug_postfix') + + ret, out = check_output('cl') + if ret == 0: + # ninja + self.check_generate('debug_postfix', args='-G Ninja') + self.check_build('debug_postfix') + self.assertTrue(os.path.exists('build/debug_postfix/foo.lib')) + self.assertTrue(os.path.exists('build/debug_postfix/hello.exe')) + shutil.rmtree('build/debug_postfix') + + self.check_generate('debug_postfix', args='-G Ninja -DCMAKE_BUILD_TYPE=Debug') + self.check_build('debug_postfix') + self.assertTrue(os.path.exists('build/debug_postfix/foo.lib')) + self.assertTrue(os.path.exists('build/debug_postfix/hello.exe')) + shutil.rmtree('build/debug_postfix') + + # Ninja Multi-Config + self.check_generate('debug_postfix', args='-G "Ninja Multi-Config"') + self.check_build('debug_postfix', '--config Debug') + self.assertTrue(os.path.exists('build/debug_postfix/Debug/foo_d.lib')) + self.assertTrue(os.path.exists('build/debug_postfix/Debug/hello_d.exe')) + shutil.rmtree('build/debug_postfix') + else: + # Ninja Multi-Config + self.check_generate('debug_postfix', args='-G "Ninja Multi-Config"') + self.check_build('debug_postfix') + self.assertTrue(os.path.exists('build/debug_postfix/Debug/libfoo_d.a')) + self.assertTrue(os.path.exists('build/debug_postfix/Debug/hello_d')) + shutil.rmtree('build/debug_postfix') + + # make + self.check_generate('debug_postfix') + self.check_build('debug_postfix') + self.assertTrue(os.path.exists('build/debug_postfix/libfoo.a')) + self.assertTrue(os.path.exists('build/debug_postfix/hello')) + shutil.rmtree('build/debug_postfix') + + self.check_generate('debug_postfix', args='-DCMAKE_BUILD_TYPE=Debug') + self.check_build('debug_postfix') + self.assertTrue(os.path.exists('build/debug_postfix/libfoo.a')) + self.assertTrue(os.path.exists('build/debug_postfix/hello')) + shutil.rmtree('build/debug_postfix') + if __name__ == "__main__": unittest.main() \ No newline at end of file diff --git a/tests/postfix/CMakeLists.txt b/tests/debug_postfix/CMakeLists.txt similarity index 55% rename from tests/postfix/CMakeLists.txt rename to tests/debug_postfix/CMakeLists.txt index 6b82575..024969f 100644 --- a/tests/postfix/CMakeLists.txt +++ b/tests/debug_postfix/CMakeLists.txt @@ -4,4 +4,7 @@ project(test_postfix) include(../../rocbuild.cmake) add_library(foo STATIC ../src/foo.c) -rocbuild_define_package(foo) \ No newline at end of file +rocbuild_set_debug_postfix(foo) + +add_executable(hello ../src/hello.c) +rocbuild_set_debug_postfix(hello) \ No newline at end of file