Skip to content

fix: some build-system cleanup #2368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ jobs:
python: 3.7
arch: x86
max-cxx-std: 14
- runs-on: windows-latest
python: 3.6
arch: x64
max-cxx-std: 17
- runs-on: windows-latest
python: 3.7
arch: x64
max-cxx-std: 17

exclude:
# Currently 32bit only, and we build 64bit
Expand Down Expand Up @@ -286,7 +294,10 @@ jobs:
apt-get install -y git make cmake g++ libeigen3-dev python3-dev python3-pip python3-pytest

- name: Configure for install
run: cmake -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") -DPYBIND11_INSTALL=1 -DPYBIND11_TEST=0 .
run: >
cmake .
-DPYBIND11_INSTALL=1 -DPYBIND11_TEST=0
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")

- name: Make and install
run: make install
Expand All @@ -298,7 +309,11 @@ jobs:
run: mkdir /build-tests

- name: Configure tests
run: cmake -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") ../pybind11-tests -DPYBIND11_WERROR=ON
run: >
cmake ../pybind11-tests
-DDOWNLOAD_CATCH=ON
-DPYBIND11_WERROR=ON
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")
working-directory: /build-tests

- name: Run tests
Expand Down
73 changes: 30 additions & 43 deletions .github/workflows/configure.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Configure
name: Config

on:
workflow_dispatch:
Expand All @@ -14,63 +14,50 @@ jobs:
strategy:
fail-fast: false
matrix:
python:
- 2.7
- 3.8
runs-on: [ubuntu-latest, macos-latest]
arch: [x64]
cmake: [3.7, 3.18]

name: CMake ${{ matrix.cmake }} Python ${{ matrix.python }} on ubuntu
runs-on: ubuntu-latest
include:
- runs-on: windows-latest
arch: x64
cmake: 3.18

# TODO: 3.8
- runs-on: windows-2016
arch: x86
cmake: 3.11

- runs-on: windows-2016
arch: x86
cmake: 3.18

name: 🐍 3.7 • CMake ${{ matrix.cmake }} • ${{ matrix.runs-on }}
runs-on: ${{ matrix.runs-on }}

steps:
- uses: actions/checkout@v2

- name: Setup Python ${{ matrix.python }}
- name: Setup Python 3.7
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
python-version: 3.7
architecture: ${{ matrix.arch }}

- name: Prepare env
run: python -m pip install -r tests/requirements.txt

- name: Make build directories
run: |
mkdir build3.7
mkdir build3.11
mkdir build3.18

- name: Setup CMake 3.7
- name: Setup CMake ${{ matrix.cmake }}
uses: jwlawson/[email protected]
with:
cmake-version: 3.7

- name: Configure 3.7
working-directory: build3.7
run: >
cmake ..
-DPYBIND11_WERROR=ON
-DDOWNLOAD_CATCH=ON
-DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)")
cmake-version: ${{ matrix.cmake }}

- name: Setup CMake 3.11
uses: jwlawson/[email protected]
with:
cmake-version: 3.11

- name: Configure 3.11
working-directory: build3.11
run: >
cmake ..
-DPYBIND11_WERROR=ON
-DDOWNLOAD_CATCH=ON
-DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)")

- name: Setup CMake 3.18
uses: jwlawson/[email protected]
with:
cmake-version: 3.18
- name: Make build directories
run: mkdir "build dir"

- name: Configure 3.18
working-directory: build3.18
- name: Configure
working-directory: build dir
shell: bash
run: >
cmake ..
-DPYBIND11_WERROR=ON
Expand Down
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ repos:
exclude: ^(docs/.*|tools/.*)$

- repo: https://github.com/cheshirekow/cmake-format-precommit
rev: v0.6.10
rev: v0.6.11
hooks:
- id: cmake-format
additional_dependencies: [pyyaml]
types: [file]
files: (\.cmake|CMakeLists.txt)(.in)?$

- repo: local
hooks:
Expand Down
22 changes: 12 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

cmake_minimum_required(VERSION 3.7)

# VERSION 3.7...3.18, but some versions of MCVS have a patched CMake 3.11
# that do not work properly with this syntax, so using the following workaround:
# The `cmake_minimum_required(VERSION 3.7...3.18)` syntax does not work with
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
# the behavior using the following workaround:
if(${CMAKE_VERSION} VERSION_LESS 3.18)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
Expand All @@ -26,7 +27,7 @@ foreach(ver ${pybind11_version_defines})
endforeach()

if(PYBIND11_VERSION_PATCH MATCHES [[([a-zA-Z]+)]])
set(PYBIND11_VERSION_TYPE "${CMAKE_MATCH_1}")
set(pybind11_VERSION_TYPE "${CMAKE_MATCH_1}")
endif()
string(REGEX MATCH "[0-9]+" PYBIND11_VERSION_PATCH "${PYBIND11_VERSION_PATCH}")

Expand All @@ -40,7 +41,7 @@ include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
include(CMakeDependentOption)

message(STATUS "pybind11 v${pybind11_VERSION} ${PYBIND11_VERSION_TYPE}")
message(STATUS "pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE}")

# Check if pybind11 is being used directly or via add_subdirectory
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
Expand All @@ -56,9 +57,11 @@ else()
set(pybind11_system SYSTEM)
endif()

# Options
option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT})
option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT})
option(PYBIND11_CLASSIC_LTO "Use the classic LTO flag algorithm, even on CMake 3.9+" OFF)

cmake_dependent_option(
USE_PYTHON_INCLUDE_DIR
"Install pybind11 headers in Python include directory instead of default installation prefix"
Expand Down Expand Up @@ -117,8 +120,7 @@ string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/" PYBIND11_HEADER

# Classic mode

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tools")
include(pybind11Tools)
include("${CMAKE_CURRENT_LIST_DIR}/tools/pybind11Tools.cmake")

# Cache variables so pybind11_add_module can be used in parent projects
set(PYBIND11_INCLUDE_DIR
Expand Down Expand Up @@ -146,10 +148,6 @@ set(PYTHON_IS_DEBUG
"${PYTHON_IS_DEBUG}"
CACHE INTERNAL "")

if(PYBIND11_TEST OR (BUILD_TESTING AND PYBIND11_MASTER_PROJECT))
add_subdirectory(tests)
endif()

if(USE_PYTHON_INCLUDE_DIR)
file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS})
endif()
Expand Down Expand Up @@ -273,3 +271,7 @@ if(PYBIND11_INSTALL)
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()
endif()

if(PYBIND11_TEST OR (BUILD_TESTING AND PYBIND11_MASTER_PROJECT))
add_subdirectory(tests)
endif()
16 changes: 8 additions & 8 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

cmake_minimum_required(VERSION 3.7)

# VERSION 3.7...3.18, but some versions of VS have a patched CMake 3.11
# that do not work properly with this syntax, so using the following workaround:
# The `cmake_minimum_required(VERSION 3.7...3.18)` syntax does not work with
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
# the behavior using the following workaround:
if(${CMAKE_VERSION} VERSION_LESS 3.18)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
Expand All @@ -18,6 +19,9 @@ endif()
# There's no harm in including a project in a project
project(pybind11_tests CXX)

# Access FindCatch and more
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../tools")

option(PYBIND11_WERROR "Report all warnings as errors" OFF)
option(DOWNLOAD_EIGEN "Download EIGEN (requires CMake 3.11+)" OFF)
set(PYBIND11_TEST_OVERRIDE
Expand Down Expand Up @@ -89,12 +93,8 @@ endif()

# Skip test_async for Python < 3.5
list(FIND PYBIND11_TEST_FILES test_async.cpp PYBIND11_TEST_FILES_ASYNC_I)
if((PYBIND11_TEST_FILES_ASYNC_I GREATER -1) AND ("${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}"
VERSION_LESS 3.5))
message(
STATUS
"Skipping test_async because Python version ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} < 3.5"
)
if((PYBIND11_TEST_FILES_ASYNC_I GREATER -1) AND (PYTHON_VERSION VERSION_LESS 3.5))
message(STATUS "Skipping test_async because Python version ${PYTHON_VERSION} < 3.5")
list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_ASYNC_I})
endif()

Expand Down
14 changes: 8 additions & 6 deletions tests/test_cmake_build/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
add_custom_target(test_cmake_build)

function(pybind11_add_build_test name)
cmake_parse_arguments(ARG "INSTALL" "" "" ${ARGN})
cmake_parse_arguments(PARSE_ARGV 1 ARG "INSTALL" "" "")

set(build_options
"-DCMAKE_PREFIX_PATH=${pybind11_BINARY_DIR}/mock_install"
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
"-DPYTHON_EXECUTABLE:FILEPATH=${PYTHON_EXECUTABLE}")

if(CMAKE_CXX_STANDARD)
if(DEFINED CMAKE_CXX_STANDARD)
list(APPEND build_options "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}")
endif()

if(NOT ARG_INSTALL)
list(APPEND build_options "-DPYBIND11_PROJECT_DIR=${pybind11_SOURCE_DIR}")
else()
list(APPEND build_options "-DCMAKE_PREFIX_PATH=${pybind11_BINARY_DIR}/mock_install")
endif()

add_custom_target(
test_${name}
test_build_${name}
${CMAKE_CTEST_COMMAND}
--build-and-test
"${CMAKE_CURRENT_SOURCE_DIR}/${name}"
Expand All @@ -32,13 +34,13 @@ function(pybind11_add_build_test name)
--build-makeprogram
${CMAKE_MAKE_PROGRAM}
--build-target
check
check_${name}
--build-options
${build_options})
if(ARG_INSTALL)
add_dependencies(test_${name} mock_install)
add_dependencies(test_build_${name} mock_install)
endif()
add_dependencies(test_cmake_build test_${name})
add_dependencies(test_cmake_build test_build_${name})
endfunction()

pybind11_add_build_test(subdirectory_function)
Expand Down
21 changes: 16 additions & 5 deletions tests/test_cmake_build/installed_embed/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
cmake_minimum_required(VERSION 3.7)

# The `cmake_minimum_required(VERSION 3.7...3.18)` syntax does not work with
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
# the behavior using the following workaround:
if(${CMAKE_VERSION} VERSION_LESS 3.18)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.18)
endif()

project(test_installed_embed CXX)

set(CMAKE_MODULE_PATH "")
find_package(pybind11 CONFIG REQUIRED)
message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}")

add_executable(test_cmake_build ../embed.cpp)
target_link_libraries(test_cmake_build PRIVATE pybind11::embed)
add_executable(test_installed_embed ../embed.cpp)
target_link_libraries(test_installed_embed PRIVATE pybind11::embed)
set_target_properties(test_installed_embed PROPERTIES OUTPUT_NAME test_cmake_build)

# Do not treat includes from IMPORTED target as SYSTEM (Python headers in pybind11::embed).
# This may be needed to resolve header conflicts, e.g. between Python release and debug headers.
set_target_properties(test_cmake_build PROPERTIES NO_SYSTEM_FROM_IMPORTED ON)
set_target_properties(test_installed_embed PROPERTIES NO_SYSTEM_FROM_IMPORTED ON)

add_custom_target(check $<TARGET_FILE:test_cmake_build> ${PROJECT_SOURCE_DIR}/../test.py)
add_custom_target(check_installed_embed $<TARGET_FILE:test_installed_embed>
${PROJECT_SOURCE_DIR}/../test.py)
22 changes: 16 additions & 6 deletions tests/test_cmake_build/installed_function/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
cmake_minimum_required(VERSION 3.7)
project(test_installed_module CXX)

set(CMAKE_MODULE_PATH "")
# The `cmake_minimum_required(VERSION 3.7...3.18)` syntax does not work with
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
# the behavior using the following workaround:
if(${CMAKE_VERSION} VERSION_LESS 3.18)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.18)
endif()

project(test_installed_function CXX)

find_package(pybind11 CONFIG REQUIRED)
message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}")
message(
STATUS "Found pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE}: ${pybind11_INCLUDE_DIRS}")

pybind11_add_module(test_cmake_build SHARED NO_EXTRAS ../main.cpp)
pybind11_add_module(test_installed_function SHARED NO_EXTRAS ../main.cpp)
set_target_properties(test_installed_function PROPERTIES OUTPUT_NAME test_cmake_build)

add_custom_target(
check
check_installed_function
${CMAKE_COMMAND}
-E
env
PYTHONPATH=$<TARGET_FILE_DIR:test_cmake_build>
PYTHONPATH=$<TARGET_FILE_DIR:test_installed_function>
${PYTHON_EXECUTABLE}
${PROJECT_SOURCE_DIR}/../test.py
${PROJECT_NAME})
Loading