diff options
| author | Egor Tensin <egor@tensin.name> | 2026-03-29 17:58:26 +0200 |
|---|---|---|
| committer | Egor Tensin <egor@tensin.name> | 2026-03-30 10:10:47 +0200 |
| commit | cbf3d06d35bc3ca194a331fd68281834f6f149a5 (patch) | |
| tree | cae61f2688d76121583567dcfe5f38b0b01cd362 | |
| parent | project.boost.build: saner command line options (diff) | |
| download | cmake-common-cbf3d06d35bc3ca194a331fd68281834f6f149a5.tar.gz cmake-common-cbf3d06d35bc3ca194a331fd68281834f6f149a5.zip | |
examples: finalize the removal of common.cmake
In addition, command line arguments have been overhauled for sanity and
consistency.
Diffstat (limited to '')
| -rw-r--r-- | examples/boost/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | examples/dynamic/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | examples/simple/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | examples/static/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | project/boost/build.py | 56 | ||||
| -rw-r--r-- | project/build.py | 53 | ||||
| -rw-r--r-- | project/linkage.py | 44 |
7 files changed, 105 insertions, 60 deletions
diff --git a/examples/boost/CMakeLists.txt b/examples/boost/CMakeLists.txt index e7ac156..0e92e38 100644 --- a/examples/boost/CMakeLists.txt +++ b/examples/boost/CMakeLists.txt @@ -1,12 +1,12 @@ -cmake_minimum_required(VERSION 3.8) +cmake_minimum_required(VERSION 3.15) project(example_boost) -include(../../common.cmake) +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Boost REQUIRED COMPONENTS filesystem) add_executable(foo foo.cpp) target_link_libraries(foo PRIVATE Boost::disable_autolinking Boost::filesystem) install(TARGETS foo RUNTIME DESTINATION bin) -install_pdbs(TARGETS foo DESTINATION bin) diff --git a/examples/dynamic/CMakeLists.txt b/examples/dynamic/CMakeLists.txt index e1e86b6..7c35f2e 100644 --- a/examples/dynamic/CMakeLists.txt +++ b/examples/dynamic/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.8) +cmake_minimum_required(VERSION 3.15) project(example_dynamic) diff --git a/examples/simple/CMakeLists.txt b/examples/simple/CMakeLists.txt index 6ab66d6..f2ba4d4 100644 --- a/examples/simple/CMakeLists.txt +++ b/examples/simple/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.8) +cmake_minimum_required(VERSION 3.15) project(example_simple) diff --git a/examples/static/CMakeLists.txt b/examples/static/CMakeLists.txt index 7af642d..c3ebbe4 100644 --- a/examples/static/CMakeLists.txt +++ b/examples/static/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.8) +cmake_minimum_required(VERSION 3.15) project(example_static) diff --git a/project/boost/build.py b/project/boost/build.py index e74004d..8ef993c 100644 --- a/project/boost/build.py +++ b/project/boost/build.py @@ -36,7 +36,6 @@ import tempfile from project.boost.directory import BoostDir from project.configuration import Configuration from project.linkage import Linkage -from project.os import on_linux_like from project.platform import Platform from project.toolset import Toolset, ToolsetVersion from project.utils import normalize_path, setup_logging @@ -45,10 +44,6 @@ import project.version DEFAULT_PLATFORMS = (Platform.AUTO,) DEFAULT_CONFIGURATIONS = (Configuration.DEBUG, Configuration.RELEASE,) -# For my development, I link everything statically (to be able to pull the -# binaries from a CI, etc. and run them everywhere): -DEFAULT_LINK = (Linkage.STATIC,) -DEFAULT_RUNTIME_LINK = Linkage.STATIC DEFAULT_TOOLSET_VERSION = ToolsetVersion.default() B2_QUIET = ['warnings=off', '-d0'] B2_VERBOSE = ['warnings=all', '-d2', '--debug-configuration'] @@ -65,8 +60,8 @@ class BuildParameters: build_dir = normalize_path(build_dir) platforms = platforms or DEFAULT_PLATFORMS configurations = configurations or DEFAULT_CONFIGURATIONS - link = link or DEFAULT_LINK - runtime_link = runtime_link or DEFAULT_RUNTIME_LINK + link = link or Linkage.default_link() + runtime_link = runtime_link or Linkage.default_runtime_link() toolset_version = toolset_version or DEFAULT_TOOLSET_VERSION verbosity = B2_VERBOSE if verbose else B2_QUIET if b2_args: @@ -94,21 +89,8 @@ class BuildParameters: with self._create_build_dir() as build_dir: for platform in self.platforms: for configuration in self.configurations: - for link, runtime_link in self._enum_linkage_options(): - with self._b2_args(build_dir, platform, configuration, link, runtime_link) as args: - yield args - - def _enum_linkage_options(self): - for link in self.link: - runtime_link = self.runtime_link - if runtime_link is Linkage.STATIC: - if link is Linkage.SHARED: - logging.warning("Cannot link the runtime statically to a dynamic library, going to link dynamically") - runtime_link = Linkage.SHARED - elif on_linux_like(): - logging.warning("Cannot link to the GNU C Library or BSD libc (which are assumed) statically, going to link dynamically") - runtime_link = Linkage.SHARED - yield link, runtime_link + with self._b2_args(build_dir, platform, configuration) as args: + yield args @contextmanager def _create_build_dir(self): @@ -126,15 +108,16 @@ class BuildParameters: return @contextmanager - def _b2_args(self, build_dir, platform, configuration, link, runtime_link): + def _b2_args(self, build_dir, platform, configuration): toolset = Toolset.make(self.toolset_version, platform) + link, runtime_link = Linkage.validate_linkage(self.link, self.runtime_link) with toolset.b2_args() as result: result.append(f'--build-dir={build_dir}') result.append('--layout=system') result += platform.b2_args(configuration) result += configuration.b2_args() - result += link.b2_args() - result += runtime_link.b2_args('runtime-link') + result += link.b2_args_link() + result += runtime_link.b2_args_runtime_link() result += self.b2_args if self.libraries: result += [f'--with-{lib}' for lib in self.libraries] @@ -159,8 +142,18 @@ def _parse_args(argv=None): description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument('--help-toolsets', action='store_true', + help='show detailed info about supported toolsets') + project.version.add_to_arg_parser(parser) + parser.add_argument('-v', '--verbose', action='store_true', + help='verbose b2 invocation (quiet by default)') + + parser.add_argument('--toolset', metavar='TOOLSET', dest='toolset_version', + type=ToolsetVersion.parse, default=DEFAULT_TOOLSET_VERSION, + help=f'toolset to use ({ToolsetVersion.usage()})') + platform_options = '/'.join(map(str, Platform.all())) configuration_options = '/'.join(map(str, Configuration.all())) # These are used to put the built libraries into proper installation @@ -177,24 +170,18 @@ def _parse_args(argv=None): # different: static & dynamic libs are built on Linux, but only static libs # are built on Windows by default. parser.add_argument('--link', metavar='LINKAGE', - nargs='*', type=Linkage.parse, default=[], + type=Linkage.parse, default=Linkage.default_link(), help=f'how the libraries are linked ({linkage_options})') # This is used to omit runtime-link=static I'd have to otherwise use a lot, # plus the script validates the link= and runtime-link= combinations. parser.add_argument('--runtime-link', metavar='LINKAGE', - type=Linkage.parse, default=DEFAULT_RUNTIME_LINK, + type=Linkage.parse, default=Linkage.default_runtime_link(), help=f'how the libraries link to the runtime ({linkage_options})') parser.add_argument('--build', metavar='DIR', dest='build_dir', type=normalize_path, help='Boost build directory (temporary directory unless specified)') - parser.add_argument('--toolset', metavar='TOOLSET', dest='toolset_version', - type=ToolsetVersion.parse, default=DEFAULT_TOOLSET_VERSION, - help=f'toolset to use ({ToolsetVersion.usage()})') - parser.add_argument('--help-toolsets', action='store_true', - help='show detailed info about supported toolsets') - parser.add_argument('--b2-arg', metavar='ARG', dest='b2_args', action='extend', nargs='*', help='additional b2 arguments, to be passed verbatim') @@ -206,9 +193,6 @@ def _parse_args(argv=None): action='extend', nargs='*', help='libraries to build (all libraries by default)') - parser.add_argument('-v', '--verbose', action='store_true', - help='verbose b2 invocation (quiet by default)') - return parser.parse_args(argv) diff --git a/project/build.py b/project/build.py index 77eb3ca..fe7fbdc 100644 --- a/project/build.py +++ b/project/build.py @@ -28,6 +28,7 @@ import sys import tempfile from project.configuration import Configuration +from project.linkage import Linkage from project.platform import Platform from project.toolset import Toolset, ToolsetVersion from project.utils import normalize_path, mkdir_parent, run, setup_logging @@ -51,15 +52,19 @@ def run_cmake(cmake_args): class GenerationPhase: - def __init__(self, src_dir, build_dir, install_dir=None, platform=None, - configuration=None, boost_dir=None, cmake_args=None): + def __init__(self, src_dir, build_dir, platform=None, configuration=None, + boost_dir=None, link=None, runtime_link=None, + install_dir=None, cmake_args=None): self.src_dir = normalize_path(src_dir) self.build_dir = normalize_path(build_dir) - self.install_dir = normalize_path(install_dir) if install_dir else None self.platform = platform or DEFAULT_PLATFORM self.configuration = configuration or DEFAULT_CONFIGURATION self.boost_dir = normalize_path(boost_dir) if boost_dir else None + link = link or Linkage.default_link() + runtime_link = runtime_link or Linkage.default_runtime_link() + self.link, self.runtime_link = Linkage.validate_linkage(link, runtime_link) + self.install_dir = normalize_path(install_dir) if install_dir else None self.cmake_args = cmake_args or [] def _cmake_args(self, toolset): @@ -68,6 +73,7 @@ class GenerationPhase: result += self.configuration.cmake_args() result += self._cmake_boost_args() result += self._cmake_extra_args() + result += self.runtime_link.cmake_args_runtime_link() result += self.cmake_args result += self._cmake_dir_args() return result @@ -77,9 +83,11 @@ class GenerationPhase: return [] root = self.boost_dir root = os.path.join(root, self.platform.boost_installdir(self.configuration)) - return [ + args = [ f'-DBOOST_ROOT={root}', ] + args += self.link.cmake_args_link() + return args @staticmethod def _cmake_extra_args(): @@ -120,9 +128,9 @@ class BuildPhase: class BuildParameters: - def __init__(self, src_dir, build_dir, install_dir=None, - platform=None, configuration=None, boost_dir=None, - toolset_version=None, cmake_args=None): + def __init__(self, src_dir, build_dir, install_dir=None, platform=None, + configuration=None, boost_dir=None, link=None, + runtime_link=None, toolset_version=None, cmake_args=None): self.src_dir = normalize_path(src_dir) self.build_dir = normalize_path(build_dir) if build_dir else None @@ -130,6 +138,8 @@ class BuildParameters: self.platform = platform or DEFAULT_PLATFORM self.configuration = configuration or DEFAULT_CONFIGURATION self.boost_dir = normalize_path(boost_dir) if boost_dir else None + self.link = link or Linkage.default_link() + self.runtime_link = runtime_link or Linkage.default_runtime_link() self.toolset_version = toolset_version or DEFAULT_TOOLSET_VERSION self.cmake_args = cmake_args or [] @@ -160,10 +170,12 @@ def build(params): toolset = Toolset.make(params.toolset_version, params.platform) gen_phase = GenerationPhase(params.src_dir, build_dir, - install_dir=params.install_dir, platform=params.platform, configuration=params.configuration, boost_dir=params.boost_dir, + link=params.link, + runtime_link=params.runtime_link, + install_dir=params.install_dir, cmake_args=params.cmake_args) gen_phase.run(toolset) build_phase = BuildPhase(build_dir, install_dir=params.install_dir, @@ -183,11 +195,14 @@ def _parse_args(argv=None): description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument('--help-toolsets', action='store_true', + help='show detailed info about supported toolsets') + project.version.add_to_arg_parser(parser) - parser.add_argument('--install', metavar='DIR', dest='install_dir', - type=normalize_path, - help='install directory') + parser.add_argument('--toolset', metavar='TOOLSET', dest='toolset_version', + type=ToolsetVersion.parse, default=DEFAULT_TOOLSET_VERSION, + help=f'toolset to use ({ToolsetVersion.usage()})') platform_options = '/'.join(map(str, Platform.all())) configuration_options = '/'.join(map(str, Configuration.all())) @@ -203,11 +218,17 @@ def _parse_args(argv=None): type=normalize_path, help='set Boost directory path') - parser.add_argument('--toolset', metavar='TOOLSET', dest='toolset_version', - type=ToolsetVersion.parse, default=DEFAULT_TOOLSET_VERSION, - help=f'toolset to use ({ToolsetVersion.usage()})') - parser.add_argument('--help-toolsets', action='store_true', - help='show detailed info about supported toolsets') + linkage_options = '/'.join(map(str, Linkage.all())) + parser.add_argument('--link', metavar='LINKAGE', + type=Linkage.parse, default=Linkage.default_link(), + help=f'how Boost libraries are linked ({linkage_options})') + parser.add_argument('--runtime-link', metavar='LINKAGE', + type=Linkage.parse, default=Linkage.default_runtime_link(), + help=f'which runtime to link to ({linkage_options})') + + parser.add_argument('--install', metavar='DIR', dest='install_dir', + type=normalize_path, + help='install directory') parser.add_argument('--cmake-arg', metavar='ARG', dest='cmake_args', action='extend', nargs='*', diff --git a/project/linkage.py b/project/linkage.py index 29e1fff..49d9d23 100644 --- a/project/linkage.py +++ b/project/linkage.py @@ -5,6 +5,9 @@ import argparse from enum import Enum +import logging + +from project.os import on_linux_like class Linkage(Enum): @@ -25,5 +28,42 @@ class Linkage(Enum): except ValueError as e: raise argparse.ArgumentTypeError(f'invalid linkage: {s}') from e - def b2_args(self, name='link'): - return [f'{name}={self}'] + # For my development, I link everything statically (to be able to pull the + # binaries from a CI, etc. and run them everywhere): + @staticmethod + def default_link(): + return Linkage.STATIC + + @staticmethod + def default_runtime_link(): + return Linkage.STATIC + + @staticmethod + def validate_linkage(link, runtime_link): + if runtime_link is Linkage.STATIC: + if link is Linkage.SHARED: + logging.warning("Cannot link the runtime statically to a dynamic library, going to link dynamically") + runtime_link = Linkage.SHARED + elif on_linux_like(): + logging.warning("Cannot link to the GNU C Library or BSD libc (which are assumed) statically, going to link dynamically") + runtime_link = Linkage.SHARED + return link, runtime_link + + def b2_args_link(self): + return [f'link={self}'] + + def b2_args_runtime_link(self): + return [f'runtime-link={self}'] + + def cmake_args_link(self): + if self is Linkage.STATIC: + return ['-DBoost_USE_STATIC_LIBS=ON'] + return [] + + def cmake_args_runtime_link(self): + if self is Linkage.STATIC: + return [ + '-DBoost_USE_STATIC_RUNTIME=ON', + '-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded$<$<CONFIG:Debug>:Debug>', + ] + return [] |
