Skip to content

Commit b81913d

Browse files
jcfrjamesobutler
authored andcommitted
cmake: Add PythonQtConfigure.h to map CMake option to public macro
Introduce `PythonQtConfigure.h` to derive `PYTHONQT_USE_RELEASE_PYTHON_FALLBACK` from the CMake option `PythonQt_USE_RELEASE_PYTHON_FALLBACK`. Export build-tree binary include dir and install the configured header so `#include <PythonQtConfigure.h>` works for both build and install trees. Stop requiring downstreams to set the macro via compile definitions; users can still override by predefining `PYTHONQT_USE_RELEASE_PYTHON_FALLBACK`.
1 parent 10846ad commit b81913d

5 files changed

Lines changed: 115 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,23 @@ foreach(qtlib ${qtlibs})
267267
endif()
268268
endforeach()
269269

270+
#-----------------------------------------------------------------------------
271+
# Configure header
272+
273+
# Map the CMake option `PythonQt_USE_RELEASE_PYTHON_FALLBACK` (camelCase)
274+
# to a plain variable used by the configured header. The public-facing
275+
# preprocessor symbol remains `PYTHONQT_USE_RELEASE_PYTHON_FALLBACK`.
276+
set(PYTHONQT_USE_RELEASE_PYTHON_FALLBACK ${PythonQt_USE_RELEASE_PYTHON_FALLBACK})
277+
configure_file(
278+
src/PythonQtConfigure.h.in
279+
${CMAKE_CURRENT_BINARY_DIR}/src/PythonQtConfigure.h
280+
)
281+
unset(PYTHONQT_USE_RELEASE_PYTHON_FALLBACK)
282+
283+
list(APPEND headers
284+
${CMAKE_CURRENT_BINARY_DIR}/src/PythonQtConfigure.h
285+
)
286+
270287
#-----------------------------------------------------------------------------
271288
# Do wrapping
272289
pythonqt_wrap_cpp(gen_moc_sources ${moc_sources})
@@ -284,8 +301,8 @@ target_compile_definitions(PythonQt
284301
PRIVATE
285302
$<$<BOOL:${PythonQt_DEBUG}>:PYTHONQT_DEBUG>
286303
$<$<BOOL:${PythonQt_SUPPORT_NAME_PROPERTY}>:PYTHONQT_SUPPORT_NAME_PROPERTY>
287-
PUBLIC
288-
$<$<BOOL:${PythonQt_USE_RELEASE_PYTHON_FALLBACK}>:PYTHONQT_USE_RELEASE_PYTHON_FALLBACK>
304+
# No need to export PYTHONQT_USE_RELEASE_PYTHON_FALLBACK publicly. the
305+
# configured header handles consumers.
289306
)
290307

291308
target_compile_options(PythonQt PRIVATE
@@ -295,6 +312,7 @@ target_compile_options(PythonQt PRIVATE
295312
target_include_directories(PythonQt
296313
PUBLIC
297314
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
315+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/src>
298316
$<INSTALL_INTERFACE:${PythonQt_INSTALL_INCLUDE_DIR}>
299317
PRIVATE
300318
# Required for use of "QtCore/private/qmetaobjectbuilder_p.h" in "PythonQt.cpp"

src/PythonQtConfigure.h.in

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
*
3+
* Copyright (C) 2025 MeVis Medical Solutions AG All Rights Reserved.
4+
* Copyright (C) 2025 Kitware, Inc.
5+
*
6+
* This library is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 2.1 of the License, or (at your option) any later version.
10+
*
11+
* This library is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* Further, this software is distributed without any warranty that it is
17+
* free of the rightful claim of any third person regarding infringement
18+
* or the like. Any license provided herein, whether implied or
19+
* otherwise, applies only to this software file. Patent licenses, if
20+
* any, provided herein do not apply to combinations of this program with
21+
* other software, or any other product whatsoever.
22+
*
23+
* You should have received a copy of the GNU Lesser General Public
24+
* License along with this library; if not, write to the Free Software
25+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26+
*
27+
* Contact information: MeVis Medical Solutions AG, Universitaetsallee 29,
28+
* 28359 Bremen, Germany or:
29+
*
30+
* http://www.mevis.de
31+
*
32+
*/
33+
34+
#ifndef _PYTHONQTCONFIGURE_H
35+
#define _PYTHONQTCONFIGURE_H
36+
37+
// If the user has not pre-defined the macro, derive it from the CMake
38+
// configure-time variable `PYTHONQT_USE_RELEASE_PYTHON_FALLBACK`, which
39+
// mirrors the `PythonQt_USE_RELEASE_PYTHON_FALLBACK` (camelCase) CMake
40+
// option.
41+
#ifndef PYTHONQT_USE_RELEASE_PYTHON_FALLBACK
42+
#cmakedefine PYTHONQT_USE_RELEASE_PYTHON_FALLBACK
43+
#endif
44+
45+
#endif

src/PythonQtPythonInclude.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#ifndef __PythonQtPythonInclude_h
3434
#define __PythonQtPythonInclude_h
3535

36+
#include <PythonQtConfigure.h>
37+
3638
// Undefine macros that features.h defines to avoid redefinition warning
3739
#ifdef _POSIX_C_SOURCE
3840
#undef _POSIX_C_SOURCE

src/generate_configure_h.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Generate a C/C++ header from a CMake .in template for use with qmake builds.
3+
4+
CMake processes .h.in files at configure time, replacing:
5+
#cmakedefine VAR -> #define VAR (when VAR is ON/defined)
6+
-> /* #undef VAR */ (when VAR is OFF/undefined)
7+
8+
This script replaces all #cmakedefine directives with #define, which
9+
matches the default PythonQt CMake option values (all defaults are ON).
10+
"""
11+
12+
import re
13+
import sys
14+
15+
16+
def main():
17+
if len(sys.argv) != 3:
18+
print(
19+
f"Usage: {sys.argv[0]} <input.h.in> <output.h>",
20+
file=sys.stderr,
21+
)
22+
sys.exit(1)
23+
24+
in_path, out_path = sys.argv[1], sys.argv[2]
25+
26+
with open(in_path) as f:
27+
content = f.read()
28+
29+
# #cmakedefine VAR [rest] -> #define VAR [rest]
30+
content = re.sub(r"^#cmakedefine\b", "#define", content, flags=re.MULTILINE)
31+
32+
with open(out_path, "w") as f:
33+
f.write(content)
34+
35+
36+
if __name__ == "__main__":
37+
main()

src/src.pro

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ QT += widgets core-private
2929

3030
INCLUDEPATH += $$PWD
3131

32+
# Generate PythonQtConfigure.h from template at qmake-configure time.
33+
# The script replaces #cmakedefine directives (CMake syntax) with #define,
34+
# enabling a pure qmake build without requiring a prior CMake run.
35+
!exists($$PWD/PythonQtConfigure.h) {
36+
win32: PYTHONQT_PYTHON = python
37+
else: PYTHONQT_PYTHON = python3
38+
system($$PYTHONQT_PYTHON $$PWD/generate_configure_h.py \
39+
$$PWD/PythonQtConfigure.h.in \
40+
$$PWD/PythonQtConfigure.h)
41+
}
42+
3243
macx {
3344
contains(QT_MAJOR_VERSION, 6) {
3445
QMAKE_APPLE_DEVICE_ARCHS = x86_64 arm64

0 commit comments

Comments
 (0)