Skip to content

Commit 04fdc44

Browse files
authored
tests: avoid putting build products into source directory (#2353)
* tests: keep source dir clean * ci: make first build inplace * ci: drop dev setting (wasn't doing anything) * tests: warn if source directory is dirty
1 parent 1729aae commit 04fdc44

File tree

6 files changed

+66
-28
lines changed

6 files changed

+66
-28
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ jobs:
1717
runs-on: [ubuntu-latest, windows-latest, macos-latest]
1818
arch: [x64]
1919
max-cxx-std: [17]
20-
dev: [false]
2120
python:
2221
- 2.7
2322
- 3.5
@@ -30,71 +29,59 @@ jobs:
3029
python: 3.6
3130
arch: x64
3231
max-cxx-std: 17
33-
dev: false
3432
args: "-DPYBIND11_FINDPYTHON=ON"
3533
- runs-on: macos-latest
3634
python: 3.7
3735
arch: x64
3836
max-cxx-std: 17
39-
dev: false
4037
args: "-DPYBIND11_FINDPYTHON=ON"
4138
- runs-on: windows-2016
4239
python: 3.7
4340
arch: x86
4441
max-cxx-std: 14
45-
dev: false
4642
- runs-on: windows-latest
4743
python: 3.6
4844
arch: x64
4945
max-cxx-std: 17
50-
dev: false
5146
args: "-DPYBIND11_FINDPYTHON=ON"
5247
- runs-on: windows-latest
5348
python: 3.7
5449
arch: x64
5550
max-cxx-std: 17
56-
dev: false
5751

5852
- runs-on: ubuntu-latest
5953
python: 3.9-dev
6054
arch: x64
6155
max-cxx-std: 17
62-
dev: true
6356
- runs-on: macos-latest
6457
python: 3.9-dev
6558
arch: x64
6659
max-cxx-std: 17
67-
dev: true
6860

6961
exclude:
7062
# Currently 32bit only, and we build 64bit
7163
- runs-on: windows-latest
7264
python: pypy2
7365
arch: x64
7466
max-cxx-std: 17
75-
dev: false
7667
- runs-on: windows-latest
7768
python: pypy3
7869
arch: x64
7970
max-cxx-std: 17
80-
dev: false
8171

8272
# Currently broken on embed_test
8373
- runs-on: windows-latest
8474
python: 3.8
8575
arch: x64
8676
max-cxx-std: 17
87-
dev: false
8877
- runs-on: windows-latest
8978
python: 3.9-dev
9079
arch: x64
9180
max-cxx-std: 17
92-
dev: false
9381

9482

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

9986
steps:
10087
- uses: actions/checkout@v2
@@ -129,24 +116,27 @@ jobs:
129116
- name: Configure C++11 ${{ matrix.args }}
130117
shell: bash
131118
run: >
132-
cmake -S . -B build
119+
cmake -S . -B .
133120
-DPYBIND11_WERROR=ON
134121
-DDOWNLOAD_CATCH=ON
135122
-DDOWNLOAD_EIGEN=ON
136123
-DCMAKE_CXX_STANDARD=11
137124
${{ matrix.args }}
138125
139126
- name: Build C++11
140-
run: cmake --build build -j 2
127+
run: cmake --build . -j 2
141128

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

145132
- name: C++11 tests
146-
run: cmake --build build --target cpptest -j 2
133+
run: cmake --build . --target cpptest -j 2
147134

148135
- name: Interface test C++11
149-
run: cmake --build build --target test_cmake_build -v
136+
run: cmake --build . --target test_cmake_build
137+
138+
- name: Clean directory
139+
run: git clean -fdx
150140

151141
- name: Configure C++${{ matrix.max-cxx-std }} ${{ matrix.args }}
152142
shell: bash

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ MANIFEST
3232
.*.swp
3333
.DS_Store
3434
/dist
35-
/build*
35+
/*build*
3636
.cache/
3737
sosize-*.txt
3838
pybind11Config*.cmake

CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,17 @@ if(NOT pybind11_FIND_QUIETLY)
4646
endif()
4747

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

5262
if(OSX AND CMAKE_VERSION VERSION_LESS 3.7)

tests/CMakeLists.txt

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ foreach(t ${PYBIND11_CROSS_MODULE_GIL_TESTS})
239239
endif()
240240
endforeach()
241241

242-
set(testdir ${CMAKE_CURRENT_SOURCE_DIR})
243242
foreach(target ${test_targets})
244243
set(test_files ${PYBIND11_TEST_FILES})
245244
if(NOT "${target}" STREQUAL "pybind11_tests")
@@ -250,6 +249,18 @@ foreach(target ${test_targets})
250249
pybind11_add_module(${target} THIN_LTO ${target}.cpp ${test_files} ${PYBIND11_HEADERS})
251250
pybind11_enable_warnings(${target})
252251

252+
if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
253+
get_property(
254+
suffix
255+
TARGET ${target}
256+
PROPERTY SUFFIX)
257+
set(source_output "${CMAKE_CURRENT_SOURCE_DIR}/${target}${suffix}")
258+
if(suffix AND EXISTS "${source_output}")
259+
message(WARNING "Output file also in source directory; "
260+
"please remove to avoid confusion: ${source_output}")
261+
endif()
262+
endif()
263+
253264
if(MSVC)
254265
target_compile_options(${target} PRIVATE /utf-8)
255266
endif()
@@ -266,10 +277,12 @@ foreach(target ${test_targets})
266277

267278
# Always write the output file directly into the 'tests' directory (even on MSVC)
268279
if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
269-
set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${testdir}")
280+
set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
281+
"${CMAKE_CURRENT_BINARY_DIR}")
270282
foreach(config ${CMAKE_CONFIGURATION_TYPES})
271283
string(TOUPPER ${config} config)
272-
set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} "${testdir}")
284+
set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config}
285+
"${CMAKE_CURRENT_BINARY_DIR}")
273286
endforeach()
274287
endif()
275288
endforeach()
@@ -293,12 +306,26 @@ if(NOT PYBIND11_PYTEST_FOUND)
293306
CACHE INTERNAL "")
294307
endif()
295308

309+
if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
310+
# This is not used later in the build, so it's okay to regenerate each time.
311+
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/pytest.ini" "${CMAKE_CURRENT_BINARY_DIR}/pytest.ini"
312+
COPYONLY)
313+
file(APPEND "${CMAKE_CURRENT_BINARY_DIR}/pytest.ini"
314+
"\ntestpaths = \"${CMAKE_CURRENT_SOURCE_DIR}\"")
315+
316+
endif()
317+
318+
# cmake 3.12 added list(transform <list> prepend
319+
# but we can't use it yet
320+
string(REPLACE "test_" "${CMAKE_CURRENT_BINARY_DIR}/test_" PYBIND11_BINARY_TEST_FILES
321+
"${PYBIND11_PYTEST_FILES}")
322+
296323
# A single command to compile and run the tests
297324
add_custom_target(
298325
pytest
299-
COMMAND ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_PYTEST_FILES}
326+
COMMAND ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_BINARY_PYTEST_FILES}
300327
DEPENDS ${test_targets}
301-
WORKING_DIRECTORY ${testdir}
328+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
302329
USES_TERMINAL)
303330

304331
if(PYBIND11_TEST_OVERRIDE)

tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@
1313

1414
import pytest
1515

16+
import env
17+
1618
# Early diagnostic for failed imports
1719
import pybind11_tests # noqa: F401
1820

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

25+
# Avoid collecting Python3 only files
26+
collect_ignore = []
27+
if env.PY2:
28+
collect_ignore.append("test_async.py")
29+
2330

2431
def _strip_and_dedent(s):
2532
"""For triple-quote strings"""

tests/test_embed/CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,22 @@ pybind11_enable_warnings(test_embed)
2121

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

24+
if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
25+
file(COPY test_interpreter.py DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
26+
endif()
27+
2428
add_custom_target(
2529
cpptest
2630
COMMAND "$<TARGET_FILE:test_embed>"
27-
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
31+
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
2832

2933
pybind11_add_module(external_module THIN_LTO external_module.cpp)
3034
set_target_properties(external_module PROPERTIES LIBRARY_OUTPUT_DIRECTORY
31-
"${CMAKE_CURRENT_SOURCE_DIR}")
35+
"${CMAKE_CURRENT_BINARY_DIR}")
3236
foreach(config ${CMAKE_CONFIGURATION_TYPES})
3337
string(TOUPPER ${config} config)
3438
set_target_properties(external_module PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config}
35-
"${CMAKE_CURRENT_SOURCE_DIR}")
39+
"${CMAKE_CURRENT_BINARY_DIR}")
3640
endforeach()
3741
add_dependencies(cpptest external_module)
3842

0 commit comments

Comments
 (0)