From 9666346ffd8cea16ef6c1a40a5cb5c48b90a3450 Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Wed, 29 Dec 2021 11:15:27 -0800 Subject: [PATCH 1/2] Define *_STATIC_DEFINE if not BUILD_SHARED_LIBS Port of ignitionrobotics/sdformat#394 to ign-cmake2. This publicly defines the *_STATIC_DEFINE symbol for a library target if BUILD_SHARED_LIBS is Off. Signed-off-by: Steve Peters --- cmake/IgnUtils.cmake | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmake/IgnUtils.cmake b/cmake/IgnUtils.cmake index 9d009892..9d1e81e1 100644 --- a/cmake/IgnUtils.cmake +++ b/cmake/IgnUtils.cmake @@ -1416,6 +1416,10 @@ macro(_ign_add_library_or_component) set(install_include_dir "${IGN_INCLUDE_INSTALL_DIR_FULL}/${include_dir}") + if (NOT BUILD_SHARED_LIBS) + target_compile_definitions(${lib_name} PUBLIC ${export_base}_STATIC_DEFINE) + endif() + # Configure the installation of the automatically generated file. install( FILES "${implementation_file_name}" From b33ab2983495da02652f8cbcfdf28177ea182999 Mon Sep 17 00:00:00 2001 From: Steve Peters Date: Wed, 29 Dec 2021 12:02:15 -0800 Subject: [PATCH 2/2] Add examples using static libraries This adds an example that builds a static library and a second example that links against that static library. Signed-off-by: Steve Peters --- examples/CMakeLists.txt | 9 ++++++ examples/core_nodep_static/CMakeLists.txt | 8 +++++ examples/core_nodep_static/README.md | 14 +++++++++ .../core_nodep_static/include/CMakeLists.txt | 0 examples/core_nodep_static/src/AlmostEmpty.cc | 29 +++++++++++++++++++ examples/core_nodep_static/src/CMakeLists.txt | 3 ++ .../core_nodep_static/test/CMakeLists.txt | 0 examples/core_static_child/CMakeLists.txt | 8 +++++ examples/core_static_child/README.md | 15 ++++++++++ .../core_static_child/include/CMakeLists.txt | 0 examples/core_static_child/src/CMakeLists.txt | 6 ++++ examples/core_static_child/src/empty.cc | 17 +++++++++++ .../core_static_child/test/CMakeLists.txt | 0 13 files changed, 109 insertions(+) create mode 100644 examples/core_nodep_static/CMakeLists.txt create mode 100644 examples/core_nodep_static/README.md create mode 100644 examples/core_nodep_static/include/CMakeLists.txt create mode 100644 examples/core_nodep_static/src/AlmostEmpty.cc create mode 100644 examples/core_nodep_static/src/CMakeLists.txt create mode 100644 examples/core_nodep_static/test/CMakeLists.txt create mode 100644 examples/core_static_child/CMakeLists.txt create mode 100644 examples/core_static_child/README.md create mode 100644 examples/core_static_child/include/CMakeLists.txt create mode 100644 examples/core_static_child/src/CMakeLists.txt create mode 100644 examples/core_static_child/src/empty.cc create mode 100644 examples/core_static_child/test/CMakeLists.txt diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 4e27100f..748e53c3 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -13,8 +13,10 @@ set(example_directories no_ignition_prefix prerelease core_nodep + core_nodep_static core_child core_child_private + core_static_child comp_deps ) if (NOT CMAKE_GENERATOR MATCHES "Visual Studio") @@ -36,12 +38,18 @@ foreach(example ${example_directories}) elseif (${example} STREQUAL "core_nodep") set(example_tarball_name ignition-core_no_deps-0.1.0.tar.bz2) set(run_codecheck true) + elseif (${example} STREQUAL "core_nodep_static") + set(example_tarball_name ignition-core_no_deps_static-0.1.0.tar.bz2) + set(run_codecheck true) elseif (${example} STREQUAL "core_child") set(example_tarball_name ignition-core_child-0.1.0.tar.bz2) set(run_codecheck true) elseif (${example} STREQUAL "core_child_private") set(example_tarball_name ignition-core_child_private-0.1.0.tar.bz2) set(run_codecheck true) + elseif (${example} STREQUAL "core_static_child") + set(example_tarball_name ignition-core_static_child-0.1.0.tar.bz2) + set(run_codecheck true) elseif (${example} STREQUAL "comp_deps") set(example_tarball_name ignition-component_deps-0.1.0.tar.bz2) elseif (${example} STREQUAL "use_component_depsA") @@ -153,6 +161,7 @@ endforeach() foreach (build_type ${build_types}) add_dependencies(core_child_${build_type} core_nodep_${build_type}) add_dependencies(core_child_private_${build_type} core_nodep_${build_type}) + add_dependencies(core_static_child_${build_type} core_nodep_static_${build_type}) if (TARGET use_component_depsA_${build_type}) add_dependencies(use_component_depsA_${build_type} comp_deps_${build_type}) endif() diff --git a/examples/core_nodep_static/CMakeLists.txt b/examples/core_nodep_static/CMakeLists.txt new file mode 100644 index 00000000..7d006f80 --- /dev/null +++ b/examples/core_nodep_static/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR) +project(ignition-core_no_deps_static VERSION 0.1.0) +find_package(ignition-cmake2 REQUIRED) +ign_configure_project() +OPTION(BUILD_SHARED_LIBS OFF) +ign_configure_build(QUIT_IF_BUILD_ERRORS) +ign_create_packages() +ign_create_docs() diff --git a/examples/core_nodep_static/README.md b/examples/core_nodep_static/README.md new file mode 100644 index 00000000..7d892d5d --- /dev/null +++ b/examples/core_nodep_static/README.md @@ -0,0 +1,14 @@ +# core\_nodep\_static example + +This package sets the `BUILD_SHARED_LIBS` cmake variable to `OFF`, +ensuring that a static library will be created. +The package has no dependencies. + +To build: + +~~~ +mkdir build +cd build +cmake .. +make +~~~ diff --git a/examples/core_nodep_static/include/CMakeLists.txt b/examples/core_nodep_static/include/CMakeLists.txt new file mode 100644 index 00000000..e69de29b diff --git a/examples/core_nodep_static/src/AlmostEmpty.cc b/examples/core_nodep_static/src/AlmostEmpty.cc new file mode 100644 index 00000000..f55af640 --- /dev/null +++ b/examples/core_nodep_static/src/AlmostEmpty.cc @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2021 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ + +#include + +namespace ignition +{ + namespace core_no_deps + { + class IGNITION_CORE_NO_DEPS_STATIC_VISIBLE AlmostEmpty + { + public: AlmostEmpty() = default; + }; + } +} diff --git a/examples/core_nodep_static/src/CMakeLists.txt b/examples/core_nodep_static/src/CMakeLists.txt new file mode 100644 index 00000000..05b85543 --- /dev/null +++ b/examples/core_nodep_static/src/CMakeLists.txt @@ -0,0 +1,3 @@ +ign_get_libsources_and_unittests(sources gtest_sources) +ign_create_core_library(SOURCES ${sources} CXX_STANDARD 11) +ign_build_tests(TYPE UNIT SOURCES ${gtest_sources}) diff --git a/examples/core_nodep_static/test/CMakeLists.txt b/examples/core_nodep_static/test/CMakeLists.txt new file mode 100644 index 00000000..e69de29b diff --git a/examples/core_static_child/CMakeLists.txt b/examples/core_static_child/CMakeLists.txt new file mode 100644 index 00000000..166828e7 --- /dev/null +++ b/examples/core_static_child/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR) +project(ignition-core_static_child VERSION 0.1.0) +find_package(ignition-cmake2 REQUIRED) +ign_configure_project() +ign_find_package(ignition-core_no_deps_static REQUIRED) +ign_configure_build(QUIT_IF_BUILD_ERRORS) +ign_create_packages() +ign_create_docs(TAGFILES "${IGNITION-CORE_NO_DEPS_STATIC_DOXYGEN_TAGFILE} = ${IGNITION-CORE_NO_DEPS_STATIC_API_URL}") diff --git a/examples/core_static_child/README.md b/examples/core_static_child/README.md new file mode 100644 index 00000000..0f85f2e9 --- /dev/null +++ b/examples/core_static_child/README.md @@ -0,0 +1,15 @@ +# core\_static\_child example + +This package links against that static library provided by +the `core_no_deps_static` package. + +To build, ensure that `core_no_deps_static` has been installed somewhere +in your `CMAKE_PREFIX_PATH` and then run: + +~~~ +mkdir build +cd build +cmake .. +make +~~~ + diff --git a/examples/core_static_child/include/CMakeLists.txt b/examples/core_static_child/include/CMakeLists.txt new file mode 100644 index 00000000..e69de29b diff --git a/examples/core_static_child/src/CMakeLists.txt b/examples/core_static_child/src/CMakeLists.txt new file mode 100644 index 00000000..400f4ecd --- /dev/null +++ b/examples/core_static_child/src/CMakeLists.txt @@ -0,0 +1,6 @@ +ign_get_libsources_and_unittests(sources gtest_sources) +ign_create_core_library(SOURCES ${sources} CXX_STANDARD 11) +target_link_libraries(${PROJECT_LIBRARY_TARGET_NAME} + PUBLIC + ignition-core_no_deps_static::ignition-core_no_deps_static) +ign_build_tests(TYPE UNIT SOURCES ${gtest_sources}) diff --git a/examples/core_static_child/src/empty.cc b/examples/core_static_child/src/empty.cc new file mode 100644 index 00000000..feb743c5 --- /dev/null +++ b/examples/core_static_child/src/empty.cc @@ -0,0 +1,17 @@ +/* + * Copyright (C) 2021 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ + diff --git a/examples/core_static_child/test/CMakeLists.txt b/examples/core_static_child/test/CMakeLists.txt new file mode 100644 index 00000000..e69de29b