Skip to content

Commit

Permalink
Switch to modern build system
Browse files Browse the repository at this point in the history
  • Loading branch information
ianhbell committed Dec 11, 2024
1 parent 236e55f commit fdcab32
Show file tree
Hide file tree
Showing 5 changed files with 485 additions and 514 deletions.
19 changes: 9 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/src/build
/build
/dist
/msvc
/notebooks/.ipynb_checkpoints
/mybuild
/PyCEGO.egg-info
/doxy
/bld
/src/CEGOversion.hpp
/src/build
/build
/dist
/msvc
/notebooks/.ipynb_checkpoints
/mybuild
/PyCEGO.egg-info
/doxy
/bld
186 changes: 101 additions & 85 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,86 +1,102 @@
set(CMAKE_OSX_ARCHITECTURES x86_64;arm64 CACHE INTERNAL "archs for osx")
cmake_minimum_required(VERSION 3.0)
project(CEGO)

# We heavily use C++17, C++17 support is required
set (CMAKE_CXX_STANDARD 17)

# Add the pybind11 stuff
if (NOT CEGO_NO_PYTHON)
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/externals/pybind11" "pybind11")
endif()

# No more CRT secure warnings...
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif()

macro(add_nix_libraries target)
# See https://stackoverflow.com/a/29871891
# Add DL and pthreads
FIND_PACKAGE ( Threads REQUIRED )
find_package(Threads REQUIRED)
if(THREADS_HAVE_PTHREAD_ARG)
target_compile_options(${target} PRIVATE "-pthread")
endif()
if(CMAKE_THREAD_LIBS_INIT)
target_link_libraries(${target} PRIVATE "${CMAKE_THREAD_LIBS_INIT}")
endif()
endmacro()

function(attach_includes target)
target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/externals/Eigen")
target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/externals/autodiff")
target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/externals/Catch/single_include")
target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/externals/ThreadPool2")
target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/externals/nlohmann")
set_target_properties(${target} PROPERTIES
CXX_STANDARD 17
CXX_EXTENSIONS OFF
)
if (NOT MSVC)
add_nix_libraries(${target})
endif()
if (MSVC)
target_sources(${target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/externals/Eigen/debug/msvc/eigen.natvis")
endif()
endfunction()

# Always build the interface target
add_library(CEGO INTERFACE)
target_include_directories(CEGO INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_include_directories(CEGO INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/externals/Eigen")
target_include_directories(CEGO INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/externals/autodiff")
target_include_directories(CEGO INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/externals/Catch/single_include")
target_include_directories(CEGO INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/externals/ThreadPool2")
target_include_directories(CEGO INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/externals/nlohmann")
#set_target_properties(CEGO PROPERTIES
# CXX_STANDARD 17
# CXX_EXTENSIONS OFF
#)

if (NOT CEGO_NO_PYTHON)
# Build pybind11 python module
pybind11_add_module(PyCEGO "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cxx")
target_compile_definitions(PyCEGO PUBLIC -DPYBIND11)
attach_includes(PyCEGO)
endif()

if (CEGO_ALL)
set(APP_SOURCES)

# Collect all the snippets in the src folder
file(GLOB_RECURSE snippets "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cxx")
message(STATUS "snippets found = ${snippets}")

foreach (snippet ${snippets})
get_filename_component(snippet_exe ${snippet} NAME_WE)
add_executable(${snippet_exe} ${snippet})
if (NOT CEGO_NO_PYTHON)
target_link_libraries (${snippet_exe} PRIVATE pybind11::embed)
endif()
attach_includes(${snippet_exe})
endforeach()

set(CMAKE_OSX_ARCHITECTURES x86_64;arm64 CACHE INTERNAL "archs for osx")
cmake_minimum_required(VERSION 3.0)
project(CEGO)

# We heavily use C++17, C++17 support is required
set (CMAKE_CXX_STANDARD 17)

# Add the pybind11 stuff
if (NOT CEGO_NO_PYTHON)
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/externals/pybind11" "pybind11")
endif()

# No more CRT secure warnings...
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif()

macro(add_nix_libraries target)
# See https://stackoverflow.com/a/29871891
# Add DL and pthreads
FIND_PACKAGE ( Threads REQUIRED )
find_package(Threads REQUIRED)
if(THREADS_HAVE_PTHREAD_ARG)
target_compile_options(${target} PRIVATE "-pthread")
endif()
if(CMAKE_THREAD_LIBS_INIT)
target_link_libraries(${target} PRIVATE "${CMAKE_THREAD_LIBS_INIT}")
endif()
endmacro()

function(attach_includes target)
target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/externals/Eigen")
target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/externals/autodiff")
target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/externals/Catch/single_include")
target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/externals/ThreadPool2")
target_include_directories(${target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/externals/nlohmann")
set_target_properties(${target} PROPERTIES
CXX_STANDARD 17
CXX_EXTENSIONS OFF
)
if (NOT MSVC)
add_nix_libraries(${target})
endif()
if (MSVC)
target_sources(${target} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/externals/Eigen/debug/msvc/eigen.natvis")
endif()
endfunction()

# Single-source the version, either from scikit, or from parsing the pyproject.toml
if (SKBUILD)
add_definitions("-DCEGOVERSION=\"${SKBUILD_PROJECT_VERSION_FULL}\"")
else()
file(READ "pyproject.toml" TOML_CONTENT)
set(REG "version = \"([0-9]+\\.[0-9]+\\.[0-9]+)\"")
string(REGEX MATCH "${REG}" VERSION_MATCH "${TOML_CONTENT}")
if (NOT VERSION_MATCH)
message(FATAL_ERROR "Can't parse the version")
else()
string(REGEX REPLACE "${REG}" "\\1" PROJECT_VERSION_FULL "${VERSION_MATCH}")
message(STATUS "Version: ${PROJECT_VERSION_FULL}")
add_definitions("-DCEGOVERSION=\"${PROJECT_VERSION_FULL}\"")
endif()
endif()

# Always build the interface target
add_library(CEGO INTERFACE)
target_include_directories(CEGO INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_include_directories(CEGO INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/externals/Eigen")
target_include_directories(CEGO INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/externals/autodiff")
target_include_directories(CEGO INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/externals/Catch/single_include")
target_include_directories(CEGO INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/externals/ThreadPool2")
target_include_directories(CEGO INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/externals/nlohmann")
#set_target_properties(CEGO PROPERTIES
# CXX_STANDARD 17
# CXX_EXTENSIONS OFF
#)

if (NOT CEGO_NO_PYTHON)
# Build pybind11 python module
pybind11_add_module(PyCEGO "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cxx")
target_compile_definitions(PyCEGO PUBLIC -DPYBIND11)
attach_includes(PyCEGO)
endif()

if (CEGO_ALL)
set(APP_SOURCES)

# Collect all the snippets in the src folder
file(GLOB_RECURSE snippets "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cxx")
message(STATUS "snippets found = ${snippets}")

foreach (snippet ${snippets})
get_filename_component(snippet_exe ${snippet} NAME_WE)
add_executable(${snippet_exe} ${snippet})
if (NOT CEGO_NO_PYTHON)
target_link_libraries (${snippet_exe} PRIVATE pybind11::embed)
endif()
attach_includes(${snippet_exe})
endforeach()

endif()
34 changes: 34 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[build-system]
requires = ["scikit-build-core >=0.4.3", "nanobind >=1.3.2", "typing_extensions"]
build-backend = "scikit_build_core.build"

[project]
name = "PyCEGO"
version = "1.2.1"
description = "C++->python wrapper of C++11 Evolutionary Global Optimization (CEGO)"
readme = "README.md"
requires-python = ">=3.8"
authors = [
{ name = "Ian Bell", email = "[email protected]" },
]
# classifiers = [
# "License :: BSD",
# ]

# [project.urls]
# Homepage = "https://github.com/your/project"

[tool.scikit-build]
# Protect the configuration against future changes in scikit-build-core
minimum-version = "0.4"

# Setuptools-style build caching in a local directory
build-dir = "build/{wheel_tag}"

# Build stable ABI wheels for CPython 3.12+
wheel.py-api = "cp312"

#cmake.verbose = true
#cmake.build-type = "Debug"
#cmake.args = ["-G Xcode", "-DXCODE_DEBUG_PYTHON=ON"]
#cmake.args = ["-DVERBOSE=ON"]
76 changes: 0 additions & 76 deletions setup.py

This file was deleted.

Loading

0 comments on commit fdcab32

Please sign in to comment.