Skip to content

Commit

Permalink
Manager project version and determine wheel name in advance
Browse files Browse the repository at this point in the history
Changes in setup.py bring support for new variables:

- If `TOPOLOGIC_VERSION` is provided as an environment variable,
then Python package and the dependency TopologicCore will obtain
provided version number, e.g. `5.0.0`.

- If `TOPOLOGIC_OUTPUT_ID` is provided as an environment variable
(with alphanumeric value), then `setup.py` will create
the file by name {TOPOLOGIC_OUTPUT_ID}.log where
a line like `WHEEL_NAME=topologic_core-5.0.0-cp312-cp312-linux_x86_64.whl`
appears. This way build recipes may get the real wheel name the line.

- Cmake supports `BUILD_VERSION` variable. For example, this can be
passed `-DBUILD_VERSION=5.0.0`. As a result, `TopologicCore/CMakeLists.txt`
will get defined version. Also, `TopologicCore/src/About.cpp` will get
the defined version (thanks to new `TopologicCore/TopologicConfig.h.in`).
  • Loading branch information
SmartCodeMaker committed May 4, 2024
1 parent 8808d64 commit bb63698
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 15 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
cmake_minimum_required(VERSION 3.24)

project(Topologic CXX)
if (BUILD_VERSION)
set(BUILD_VERSION ${BUILD_VERSION})
unset(BUILD_VERSION CACHE)
else()
set(BUILD_VERSION 5.0.0) # Default version if not set in BUILD_VERSION.
endif()

project(Topologic VERSION ${BUILD_VERSION} LANGUAGES CXX)
message("CMAKE_PROJECT_VERSION=${CMAKE_PROJECT_VERSION}")

# Sub-projects
add_subdirectory(TopologicCore)
Expand Down
8 changes: 6 additions & 2 deletions TopologicCore/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.24)

project(TopologicCore CXX)
project(TopologicCore VERSION ${CMAKE_PROJECT_VERSION} LANGUAGES CXX)
set(PROJECT_NAMESPACE Topologic)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
Expand Down Expand Up @@ -160,7 +160,11 @@ target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_14)
# Include dir
target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include)
# Set version: for now sync manually with: TopologicCore/src/About.cpp
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION "5.0.0" SOVERSION "5")
string(REGEX MATCH "^([0-9]+)\\." CMAKE_PROJECT_VERSION_MATCH ${CMAKE_PROJECT_VERSION})
set(BUILD_SOVERSION ${CMAKE_MATCH_1}) # Set "5" if project version is "5.0.0".
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${CMAKE_PROJECT_VERSION} SOVERSION ${BUILD_SOVERSION})
include_directories(${CMAKE_BINARY_DIR})
configure_file("TopologicConfig.h.in" "${CMAKE_BINARY_DIR}/TopologicConfig.h")

# Dependency: OpenCASCADE
find_package(OpenCASCADE REQUIRED)
Expand Down
16 changes: 16 additions & 0 deletions TopologicCore/TopologicConfig.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#define PROJECT_NAME "@PROJECT_NAME@"
#define PROJECT_VERSION "@PROJECT_VERSION@"
#define MAJOR_VERSION @PROJECT_VERSION_MAJOR@
#define MINOR_VERSION @PROJECT_VERSION_MINOR@
#define BUGFIX_VERSION @PROJECT_VERSION_PATCH@
#define INTERNAL_BUILD_VERSION_ @PROJECT_VERSION_TWEAK@

#if ~(~INTERNAL_BUILD_VERSION_ + 0) == 0 && ~(~INTERNAL_BUILD_VERSION_ + 1) == 1
// INTERNAL_BUILD_VERSION_ is defined, but its value is "empty".
# define INTERNAL_BUILD_VERSION 0
#else
# define INTERNAL_BUILD_VERSION INTERNAL_BUILD_VERSION_
#endif
#undef INTERNAL_BUILD_VERSION_
6 changes: 1 addition & 5 deletions TopologicCore/src/About.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#include "About.h"

#define MAJOR_VERSION 5
#define MINOR_VERSION 0
#define BUGFIX_VERSION 0
#define INTERNAL_BUILD_VERSION 1
#include "TopologicConfig.h"

namespace TopologicCore
{
Expand Down
36 changes: 32 additions & 4 deletions TopologicPythonBindings/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext

from setuptools.dist import Distribution

# taken from: https://github.com/pybind/cmake_example/blob/master/setup.py
# A CMakeExtension needs a sourcedir instead of a file list.
Expand Down Expand Up @@ -55,6 +55,7 @@ def build_extension(self, ext: CMakeExtension) -> None:
"--source-dir", source_dir,
"--build-dir", build_dir, "--install-to", install_to,
"--build-target", build_target, "--install-component", install_component,
"--build-version", self.distribution.get_version(),
"--extra-cmake-args", extra_cmake_args],
check=True
)
Expand All @@ -79,9 +80,33 @@ def build_options():
}
return {};

setup(
def try_read_actual_version(default_version):
if "TOPOLOGIC_VERSION" in os.environ:
return os.environ["TOPOLOGIC_VERSION"]
return default_version

def print_wheel_name_to_output(**kwargs):
if "TOPOLOGIC_OUTPUT_ID" not in os.environ:
return
output_name = os.environ["TOPOLOGIC_OUTPUT_ID"];
if not output_name.isalnum():
return
output_name += '.log'

dist = Distribution(attrs=kwargs)
command_obj = dist.get_command_obj('bdist_wheel')
command_obj.ensure_finalized()
dist_name = command_obj.wheel_dist_name
tag = '-'.join(command_obj.get_tag())
f = open(output_name, "w")
wheel_name = f"{dist_name}-{tag}.whl"
print(f"Expected wheel name is {wheel_name} (WHEEL_NAME={wheel_name})");
f.write(f'WHEEL_NAME={wheel_name}\n');
f.close()

setup_kwargs = dict(
name="topologic",
version="5.0.0", # for now sync manually with: TopologicCore/CMakeLists.txt and TopologicCore/src/About.cpp
version=try_read_actual_version(default_version="5.0.0"), # Syncs with: TopologicCore/CMakeLists.txt and TopologicCore/src/About.cpp
author="Topologic Authors",
author_email="None",
description="TopologicPythonBindings wrapper package",
Expand All @@ -94,4 +119,7 @@ def build_options():
extras_require={},
python_requires=">=3.8",
options=build_options()
)
);

print_wheel_name_to_output(**setup_kwargs)
setup(**setup_kwargs)
7 changes: 4 additions & 3 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _cmd_install(args):

def build_win32(args):
# letting cmake decide the generator on windows
cmd = f"cmake -S '{args.source_dir}' -B '{args.build_dir}' -DCMAKE_BUILD_TYPE='{args.build_type}' -DCMAKE_CONFIGURATION_TYPES='{args.build_type}'"
cmd = f"cmake -S '{args.source_dir}' -B '{args.build_dir}' -DCMAKE_BUILD_TYPE='{args.build_type}' -DCMAKE_CONFIGURATION_TYPES='{args.build_type}' -DBUILD_VERSION='{args.build_version}'"
cmd = _cmd_gen_add_extra_args(cmd, args)
syscmd(cmd)
cmd = f"cmake --build '{args.build_dir}' --config '{args.build_type}'"
Expand All @@ -73,7 +73,7 @@ def build_win32(args):

def build_linux(args):
# use ninja on linux
cmd = f"cmake -S '{args.source_dir}' -G Ninja -B '{args.build_dir}' -DCMAKE_BUILD_TYPE='{args.build_type}'"
cmd = f"cmake -S '{args.source_dir}' -G Ninja -B '{args.build_dir}' -DCMAKE_BUILD_TYPE='{args.build_type}' -DBUILD_VERSION='{args.build_version}'"
cmd = _cmd_gen_add_extra_args(cmd, args)
syscmd(cmd)
cmd = f"cmake --build '{args.build_dir}'"
Expand All @@ -85,7 +85,7 @@ def build_linux(args):

def build_darwin(args):
# use ninja on moacos
cmd = f"cmake -S '{args.source_dir}' -G Ninja -B '{args.build_dir}' -DCMAKE_BUILD_TYPE='{args.build_type}'"
cmd = f"cmake -S '{args.source_dir}' -G Ninja -B '{args.build_dir}' -DCMAKE_BUILD_TYPE='{args.build_type}' -DBUILD_VERSION='{args.build_version}'"
cmd = _cmd_gen_add_extra_args(cmd, args)
syscmd(cmd)
cmd = f"cmake --build '{args.build_dir}'"
Expand Down Expand Up @@ -138,6 +138,7 @@ def script():
p.add_argument("--install-component", required=False, metavar="<name>")
p.add_argument("--dry-run", action='store_true')
p.add_argument("--verbose", action='store_true')
p.add_argument("--build-version", required=False, metavar="<version>")
p.add_argument('--extra-cmake-args', nargs=argparse.REMAINDER, metavar="<args>")
args = p.parse_args()

Expand Down

0 comments on commit bb63698

Please sign in to comment.