Skip to content

tests: avoid putting build products into source directory #2353

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 4 commits into from
Aug 19, 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
26 changes: 8 additions & 18 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
runs-on: [ubuntu-latest, windows-latest, macos-latest]
arch: [x64]
max-cxx-std: [17]
dev: [false]
python:
- 2.7
- 3.5
Expand All @@ -30,71 +29,59 @@ jobs:
python: 3.6
arch: x64
max-cxx-std: 17
dev: false
args: "-DPYBIND11_FINDPYTHON=ON"
- runs-on: macos-latest
python: 3.7
arch: x64
max-cxx-std: 17
dev: false
args: "-DPYBIND11_FINDPYTHON=ON"
- runs-on: windows-2016
python: 3.7
arch: x86
max-cxx-std: 14
dev: false
- runs-on: windows-latest
python: 3.6
arch: x64
max-cxx-std: 17
dev: false
args: "-DPYBIND11_FINDPYTHON=ON"
- runs-on: windows-latest
python: 3.7
arch: x64
max-cxx-std: 17
dev: false

- runs-on: ubuntu-latest
python: 3.9-dev
arch: x64
max-cxx-std: 17
dev: true
- runs-on: macos-latest
python: 3.9-dev
arch: x64
max-cxx-std: 17
dev: true

exclude:
# Currently 32bit only, and we build 64bit
- runs-on: windows-latest
python: pypy2
arch: x64
max-cxx-std: 17
dev: false
- runs-on: windows-latest
python: pypy3
arch: x64
max-cxx-std: 17
dev: false

# Currently broken on embed_test
- runs-on: windows-latest
python: 3.8
arch: x64
max-cxx-std: 17
dev: false
- runs-on: windows-latest
python: 3.9-dev
arch: x64
max-cxx-std: 17
dev: false


name: "🐍 ${{ matrix.python }} • ${{ matrix.runs-on }} • ${{ matrix.arch }} ${{ matrix.args }}"
runs-on: ${{ matrix.runs-on }}
continue-on-error: ${{ matrix.dev }}

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -129,24 +116,27 @@ jobs:
- name: Configure C++11 ${{ matrix.args }}
shell: bash
run: >
cmake -S . -B build
cmake -S . -B .
-DPYBIND11_WERROR=ON
-DDOWNLOAD_CATCH=ON
-DDOWNLOAD_EIGEN=ON
-DCMAKE_CXX_STANDARD=11
${{ matrix.args }}

- name: Build C++11
run: cmake --build build -j 2
run: cmake --build . -j 2

- name: Python tests C++11
run: cmake --build build --target pytest -j 2
run: cmake --build . --target pytest -j 2

- name: C++11 tests
run: cmake --build build --target cpptest -j 2
run: cmake --build . --target cpptest -j 2

- name: Interface test C++11
run: cmake --build build --target test_cmake_build -v
run: cmake --build . --target test_cmake_build

- name: Clean directory
run: git clean -fdx

- name: Configure C++${{ matrix.max-cxx-std }} ${{ matrix.args }}
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ MANIFEST
.*.swp
.DS_Store
/dist
/build*
/*build*
.cache/
sosize-*.txt
pybind11Config*.cmake
Expand Down
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ if(NOT pybind11_FIND_QUIETLY)
endif()

# Check if pybind11 is being used directly or via add_subdirectory
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
### Warn if not an out-of-source builds
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
set(lines
"You are building in-place. If that is not what you intended to "
"do, you can clean the source directory with:\n"
"rm -r CMakeCache.txt CMakeFiles/ cmake_uninstall.cmake pybind11Config.cmake "
"pybind11ConfigVersion.cmake tests/CMakeFiles/\n")
message(AUTHOR_WARNING ${lines})
endif()

set(PYBIND11_MASTER_PROJECT ON)

if(OSX AND CMAKE_VERSION VERSION_LESS 3.7)
Expand Down
37 changes: 32 additions & 5 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ foreach(t ${PYBIND11_CROSS_MODULE_GIL_TESTS})
endif()
endforeach()

set(testdir ${CMAKE_CURRENT_SOURCE_DIR})
foreach(target ${test_targets})
set(test_files ${PYBIND11_TEST_FILES})
if(NOT "${target}" STREQUAL "pybind11_tests")
Expand All @@ -250,6 +249,18 @@ foreach(target ${test_targets})
pybind11_add_module(${target} THIN_LTO ${target}.cpp ${test_files} ${PYBIND11_HEADERS})
pybind11_enable_warnings(${target})

if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
get_property(
suffix
TARGET ${target}
PROPERTY SUFFIX)
set(source_output "${CMAKE_CURRENT_SOURCE_DIR}/${target}${suffix}")
if(suffix AND EXISTS "${source_output}")
message(WARNING "Output file also in source directory; "
"please remove to avoid confusion: ${source_output}")
endif()
endif()

if(MSVC)
target_compile_options(${target} PRIVATE /utf-8)
endif()
Expand All @@ -266,10 +277,12 @@ foreach(target ${test_targets})

# Always write the output file directly into the 'tests' directory (even on MSVC)
if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${testdir}")
set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
"${CMAKE_CURRENT_BINARY_DIR}")
foreach(config ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER ${config} config)
set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} "${testdir}")
set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config}
"${CMAKE_CURRENT_BINARY_DIR}")
endforeach()
endif()
endforeach()
Expand All @@ -293,12 +306,26 @@ if(NOT PYBIND11_PYTEST_FOUND)
CACHE INTERNAL "")
endif()

if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
# This is not used later in the build, so it's okay to regenerate each time.
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/pytest.ini" "${CMAKE_CURRENT_BINARY_DIR}/pytest.ini"
COPYONLY)
file(APPEND "${CMAKE_CURRENT_BINARY_DIR}/pytest.ini"
"\ntestpaths = \"${CMAKE_CURRENT_SOURCE_DIR}\"")

endif()

# cmake 3.12 added list(transform <list> prepend
# but we can't use it yet
string(REPLACE "test_" "${CMAKE_CURRENT_BINARY_DIR}/test_" PYBIND11_BINARY_TEST_FILES
"${PYBIND11_PYTEST_FILES}")

# A single command to compile and run the tests
add_custom_target(
pytest
COMMAND ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_PYTEST_FILES}
COMMAND ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_BINARY_PYTEST_FILES}
DEPENDS ${test_targets}
WORKING_DIRECTORY ${testdir}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
USES_TERMINAL)

if(PYBIND11_TEST_OVERRIDE)
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@

import pytest

import env

# Early diagnostic for failed imports
import pybind11_tests # noqa: F401

_unicode_marker = re.compile(r'u(\'[^\']*\')')
_long_marker = re.compile(r'([0-9])L')
_hexadecimal = re.compile(r'0x[0-9a-fA-F]+')

# Avoid collecting Python3 only files
collect_ignore = []
if env.PY2:
collect_ignore.append("test_async.py")


def _strip_and_dedent(s):
"""For triple-quote strings"""
Expand Down
10 changes: 7 additions & 3 deletions tests/test_embed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ pybind11_enable_warnings(test_embed)

target_link_libraries(test_embed PRIVATE pybind11::embed Catch2::Catch2 Threads::Threads)

if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
file(COPY test_interpreter.py DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
endif()

add_custom_target(
cpptest
COMMAND "$<TARGET_FILE:test_embed>"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")

pybind11_add_module(external_module THIN_LTO external_module.cpp)
set_target_properties(external_module PROPERTIES LIBRARY_OUTPUT_DIRECTORY
"${CMAKE_CURRENT_SOURCE_DIR}")
"${CMAKE_CURRENT_BINARY_DIR}")
foreach(config ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER ${config} config)
set_target_properties(external_module PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config}
"${CMAKE_CURRENT_SOURCE_DIR}")
"${CMAKE_CURRENT_BINARY_DIR}")
endforeach()
add_dependencies(cpptest external_module)

Expand Down