Skip to content

Commit

Permalink
Merge pull request NREL#8832 from NREL/FindPython
Browse files Browse the repository at this point in the history
Fix NREL#8778 - FindPython in modern cmake and handle issue when LINK_WITH_PYTHON on windows with both Release/debug libraries
  • Loading branch information
Myoldmopar authored Jul 1, 2021
2 parents cba9ba2 + 1656f7b commit 4708fe7
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 31 deletions.
11 changes: 8 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cmake_policy(SET CMP0048 NEW) # handling project_version_* variables

project(EnergyPlus)

cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.17)

if(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} VERSION_GREATER "3.0")
cmake_policy(SET CMP0054 NEW) # CMake 3.1 added this policy
Expand Down Expand Up @@ -154,7 +154,12 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
endif()

# we are making *a Python 3.6 Interpreter* a required dependency, so find it here
find_package(PythonInterp 3.6 REQUIRED)
# If LINK_WITH_PYTHON, also request the Development (libs) at the same time, to ensure consistent version between interpreter and Development
if(LINK_WITH_PYTHON)
find_package(Python 3.6 COMPONENTS Interpreter Development REQUIRED)
else()
find_package(Python 3.6 COMPONENTS Interpreter REQUIRED)
endif()

if(BUILD_TESTING)
option(ENABLE_REGRESSION_TESTING "Enable Regression Tests" OFF)
Expand Down Expand Up @@ -270,7 +275,7 @@ endif()
add_subdirectory(idd)

execute_process(
COMMAND ${PYTHON_EXECUTABLE} "${PROJECT_SOURCE_DIR}/scripts/dev/generate_epJSON_schema/generate_epJSON_schema.py" "${PROJECT_SOURCE_DIR}"
COMMAND ${Python_EXECUTABLE} "${PROJECT_SOURCE_DIR}/scripts/dev/generate_epJSON_schema/generate_epJSON_schema.py" "${PROJECT_SOURCE_DIR}"
TIMEOUT 30
RESULT_VARIABLE generate_epJSON_schema_result)
if(${generate_epJSON_schema_result} MATCHES ".*timeout.*")
Expand Down
8 changes: 4 additions & 4 deletions cmake/Install.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,18 @@ file(MAKE_DIRECTORY ${DOCS_OUT})

# the output variables listing
install(
CODE "execute_process(COMMAND \"${PYTHON_EXECUTABLE}\" \"${PROJECT_SOURCE_DIR}/doc/tools/parse_output_variables.py\" \"${PROJECT_SOURCE_DIR}/src/EnergyPlus\" \"${DOCS_OUT}/SetupOutputVariables.csv\" \"${DOCS_OUT}/SetupOutputVariables.md\")"
CODE "execute_process(COMMAND \"${Python_EXECUTABLE}\" \"${PROJECT_SOURCE_DIR}/doc/tools/parse_output_variables.py\" \"${PROJECT_SOURCE_DIR}/src/EnergyPlus\" \"${DOCS_OUT}/SetupOutputVariables.csv\" \"${DOCS_OUT}/SetupOutputVariables.md\")"
)
install(FILES "${PROJECT_BINARY_DIR}/autodocs/SetupOutputVariables.csv" DESTINATION "./")

# the example file summary
install(
CODE "execute_process(COMMAND \"${PYTHON_EXECUTABLE}\" \"${PROJECT_SOURCE_DIR}/doc/tools/example_file_summary.py\" \"${PROJECT_SOURCE_DIR}/testfiles\" \"${DOCS_OUT}/ExampleFiles.html\")"
CODE "execute_process(COMMAND \"${Python_EXECUTABLE}\" \"${PROJECT_SOURCE_DIR}/doc/tools/example_file_summary.py\" \"${PROJECT_SOURCE_DIR}/testfiles\" \"${DOCS_OUT}/ExampleFiles.html\")"
COMPONENT ExampleFiles)
install(FILES "${DOCS_OUT}/ExampleFiles.html" DESTINATION "./ExampleFiles/" COMPONENT ExampleFiles)

# the example file objects link
install(CODE "execute_process(COMMAND \"${PYTHON_EXECUTABLE}\" \"${PROJECT_SOURCE_DIR}/doc/tools/example_file_objects.py\"
install(CODE "execute_process(COMMAND \"${Python_EXECUTABLE}\" \"${PROJECT_SOURCE_DIR}/doc/tools/example_file_objects.py\"
\"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Energy+.idd\" \"${PROJECT_SOURCE_DIR}/testfiles\" \"${DOCS_OUT}/ExampleFiles-ObjectsLink.html\")"
COMPONENT ExampleFiles)
install(FILES "${DOCS_OUT}/ExampleFiles-ObjectsLink.html" DESTINATION "./ExampleFiles/" COMPONENT ExampleFiles)
Expand All @@ -252,7 +252,7 @@ if(BUILD_CHANGELOG)
# Better to move this condition into the install CODE.
if(NOT "$ENV{GITHUB_TOKEN}" STREQUAL "")
install(
CODE "execute_process(COMMAND \"${PYTHON_EXECUTABLE}\" \"${PROJECT_SOURCE_DIR}/doc/tools/create_changelog.py\" \"${PROJECT_SOURCE_DIR}\" \"${DOCS_OUT}/changelog.md\" \"${DOCS_OUT}/changelog.html\" \"${GIT_EXECUTABLE}\" \"$ENV{GITHUB_TOKEN}\" \"${PREV_RELEASE_SHA}\" \"${CPACK_PACKAGE_VERSION}\")"
CODE "execute_process(COMMAND \"${Python_EXECUTABLE}\" \"${PROJECT_SOURCE_DIR}/doc/tools/create_changelog.py\" \"${PROJECT_SOURCE_DIR}\" \"${DOCS_OUT}/changelog.md\" \"${DOCS_OUT}/changelog.html\" \"${GIT_EXECUTABLE}\" \"$ENV{GITHUB_TOKEN}\" \"${PREV_RELEASE_SHA}\" \"${CPACK_PACKAGE_VERSION}\")"
)
install(FILES "${DOCS_OUT}/changelog.html" DESTINATION "./" OPTIONAL)
else()
Expand Down
2 changes: 1 addition & 1 deletion cmake/ProjectMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function(ADD_SIMULATION_TEST)
add_test(
NAME "regression.${IDF_NAME}"
COMMAND
${CMAKE_COMMAND} -DBINARY_DIR=${PROJECT_BINARY_DIR} -DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE} -DIDF_FILE=${ADD_SIM_TEST_IDF_FILE}
${CMAKE_COMMAND} -DBINARY_DIR=${PROJECT_BINARY_DIR} -DPYTHON_EXECUTABLE=${Python_EXECUTABLE} -DIDF_FILE=${ADD_SIM_TEST_IDF_FILE}
-DREGRESSION_SCRIPT_PATH=${REGRESSION_SCRIPT_PATH} -DREGRESSION_BASELINE_PATH=${REGRESSION_BASELINE_PATH}
-DREGRESSION_BASELINE_SHA=${REGRESSION_BASELINE_SHA} -DCOMMIT_SHA=${COMMIT_SHA} -DDEVICE_ID=${DEVICE_ID} -P
${PROJECT_SOURCE_DIR}/cmake/RunRegression.cmake)
Expand Down
8 changes: 4 additions & 4 deletions doc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
project(EnergyPlusDocs LANGUAGES NONE)

cmake_minimum_required(VERSION 3.5.1)
cmake_minimum_required(VERSION 3.12)

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

if(NOT EXISTS ${PYTHON_EXECUTABLE})
# we are making *a Python 3.6 Interpreter* a required dependency, so find it here
find_package(PythonInterp 3.6 REQUIRED)
if(NOT EXISTS ${Python_EXECUTABLE})
# If running docs standalone, Python isn't found yet.
find_package(Python 3.6 COMPONENTS Interpreter REQUIRED)
endif()

option(DOCS_TESTING "Test the docs for issues" OFF)
Expand Down
2 changes: 1 addition & 1 deletion doc/cmake/doc-targets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ macro( CREATE_DOC_TARGET SOURCE_FILENAME OUTPUT_FILENAME )
if (DOCS_TESTING)
add_custom_command(TARGET zPDF_${OUTPUT_FILENAME}
POST_BUILD
COMMAND ${PYTHON_EXECUTABLE} "${PROJECT_SOURCE_DIR}/tools/parse_latex_log.py" "${PROJECT_SOURCE_DIR}/${SOURCE_FILENAME}/${SOURCE_FILENAME}.log" "${PROJECT_SOURCE_DIR}/${SOURCE_FILENAME}" "${PROJECT_BINARY_DIR}/${OUTPUT_FILENAME}_errors.json"
COMMAND ${Python_EXECUTABLE} "${PROJECT_SOURCE_DIR}/tools/parse_latex_log.py" "${PROJECT_SOURCE_DIR}/${SOURCE_FILENAME}/${SOURCE_FILENAME}.log" "${PROJECT_SOURCE_DIR}/${SOURCE_FILENAME}" "${PROJECT_BINARY_DIR}/${OUTPUT_FILENAME}_errors.json"
)
endif()
endmacro()
50 changes: 34 additions & 16 deletions src/EnergyPlus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ if(LINK_WITH_PYTHON)
# to match up bitwise if we aren't, then the identified interpreter can be
# whatever bit it wants
if(MSVC)
execute_process(COMMAND "${PYTHON_EXECUTABLE}" "${PROJECT_SOURCE_DIR}/cmake/PythonGetBitSize.py" OUTPUT_VARIABLE PYTHON_BIT_SIZE)
execute_process(COMMAND "${Python_EXECUTABLE}" "${PROJECT_SOURCE_DIR}/cmake/PythonGetBitSize.py" OUTPUT_VARIABLE PYTHON_BIT_SIZE)
# PYTHON_BIT_SIZE will be either "32" or "64"
# CMAKE_EXE_LINKER_FLAGS:STRING=/machine:x64
# CMAKE_EXE_LINKER_FLAGS:STRING=/machine:X86
Expand All @@ -740,10 +740,9 @@ if(LINK_WITH_PYTHON)
endif()
endif()

# now we need to get the Python libs and add the include path for further
# compiling
find_package(PythonLibs 3 REQUIRED)
include_directories("${PYTHON_INCLUDE_DIRS}")
# now we need to get the Python libs and add the include path for further compiling
# The Development (libs) was already found in the root level CMakeLists.txt
include_directories("${Python_INCLUDE_DIRS}")
endif()

add_library(energyplusparser STATIC ${INPUTPARSING_SRC})
Expand Down Expand Up @@ -800,7 +799,7 @@ if(UNIX AND NOT APPLE)
endif()
endif()
if(LINK_WITH_PYTHON)
target_link_libraries(energypluslib PUBLIC ${PYTHON_LIBRARIES})
target_link_libraries(energypluslib PUBLIC ${Python_LIBRARIES})
endif()
# See https://en.cppreference.com/w/cpp/filesystem#Notes
if ((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.1) OR
Expand Down Expand Up @@ -880,8 +879,27 @@ install(TARGETS energyplus energyplusapi DESTINATION ./)

if(LINK_WITH_PYTHON)
# link the executable to the dll and bring it in
get_filename_component(RESOLVED_PYTHON_LIBRARY "${PYTHON_LIBRARIES}" REALPATH)
get_filename_component(RESOLVED_PYTHON_LIBRARY "${Python_LIBRARIES}" REALPATH)
if(WIN32)

# In case you have both release and debug Python libraries on your system, Python_LIBRARIES might be "optimized;C:/.../python38.lib;debug;C:/.../python38_d.lib"
# so it can't be used directly, we could use a generator expression to find the $<TARGET_FILE:Python::Python> library used and that'd point directly
# to the DLL. But it won't work nicely for the install(code ...) command below... so we do a hack, get the optimized one always...
list(LENGTH Python_LIBRARIES _LEN)
if (_LEN GREATER 1)
set (_Python_DOING_RELEASE FALSE)
foreach(_currentArg ${Python_LIBRARIES})
if ("x${_currentArg}" STREQUAL "xoptimized")
set(_Python_DOING_RELEASE TRUE)
elseif(_Python_DOING_RELEASE)
get_filename_component(RESOLVED_PYTHON_LIBRARY "${_currentArg}" REALPATH)
break()
endif()
endforeach()
# else()
# No-op, already done above
endif()

# Windows is being ugly and linking against the Python DLL but not running
# unless it's close by Force it here.
add_custom_command(
Expand Down Expand Up @@ -912,7 +930,7 @@ if(LINK_WITH_PYTHON)
TARGET energyplusapi
POST_BUILD # TODO: I don't think we want to quote the generator expression
# here
COMMAND ${PYTHON_EXECUTABLE} "${PROJECT_SOURCE_DIR}/cmake/PythonCopyStandardLib.py" "$<TARGET_FILE:energyplusapi>" "python_standard_lib")
COMMAND ${Python_EXECUTABLE} "${PROJECT_SOURCE_DIR}/cmake/PythonCopyStandardLib.py" "$<TARGET_FILE:energyplusapi>" "python_standard_lib")
endif()

if(BUILD_PACKAGE)
Expand Down Expand Up @@ -981,30 +999,30 @@ if(BUILD_TESTING)
configure_file("${PROJECT_SOURCE_DIR}/tst/EnergyPlus/api/TestFunctional.py" "${API_TEST_TARGET_DIR}/TestFunctional.py")
add_test(
NAME "API.TestFunctionalPython"
COMMAND "${PYTHON_EXECUTABLE}" "${API_TEST_TARGET_DIR}/TestFunctional.py"
COMMAND "${Python_EXECUTABLE}" "${API_TEST_TARGET_DIR}/TestFunctional.py"
WORKING_DIRECTORY "${API_TEST_TARGET_DIR}")

set(TEST_DIR "${PROJECT_BINARY_DIR}/tst/api/runtime_python")
file(MAKE_DIRECTORY ${TEST_DIR})
configure_file("${PROJECT_SOURCE_DIR}/tst/EnergyPlus/api/TestRuntime.py" "${API_TEST_TARGET_DIR}/TestRuntime.py")
add_test(
NAME "API.TestRuntimePython"
COMMAND "${PYTHON_EXECUTABLE}" "${API_TEST_TARGET_DIR}/TestRuntime.py" -d "${TEST_DIR}" -w "${EPW_FILE}" -D "${IDF_FILE}"
COMMAND "${Python_EXECUTABLE}" "${API_TEST_TARGET_DIR}/TestRuntime.py" -d "${TEST_DIR}" -w "${EPW_FILE}" -D "${IDF_FILE}"
WORKING_DIRECTORY "${API_TEST_TARGET_DIR}")

set(TEST_DIR "${PROJECT_BINARY_DIR}/tst/api/exchange_python")
file(MAKE_DIRECTORY ${TEST_DIR})
configure_file("${PROJECT_SOURCE_DIR}/tst/EnergyPlus/api/TestDataTransfer.py" "${API_TEST_TARGET_DIR}/TestDataTransfer.py")
add_test(
NAME "API.TestDataTransferPython"
COMMAND "${PYTHON_EXECUTABLE}" "${API_TEST_TARGET_DIR}/TestDataTransfer.py" -d "${TEST_DIR}" -w "${EPW_FILE}" -D "${IDF_FILE}"
COMMAND "${Python_EXECUTABLE}" "${API_TEST_TARGET_DIR}/TestDataTransfer.py" -d "${TEST_DIR}" -w "${EPW_FILE}" -D "${IDF_FILE}"
WORKING_DIRECTORY "${API_TEST_TARGET_DIR}")

# set(TEST_DIR "${PROJECT_BINARY_DIR}/tst/api/autosizing_python")
# file(MAKE_DIRECTORY ${TEST_DIR}) configure_file(
# "${PROJECT_SOURCE_DIR}/tst/EnergyPlus/api/TestAutosizing.py"
# "${API_TEST_TARGET_DIR}/TestAutosizing.py" ) add_test(NAME
# "API.TestAutosizingPython" COMMAND "${PYTHON_EXECUTABLE}"
# "API.TestAutosizingPython" COMMAND "${Python_EXECUTABLE}"
# "${API_TEST_TARGET_DIR}/TestAutosizing.py" -d "${TEST_DIR}" -w
# "${EPW_FILE}" -D "${IDF_FILE}" WORKING_DIRECTORY "${API_TEST_TARGET_DIR}")

Expand All @@ -1013,25 +1031,25 @@ if(BUILD_TESTING)
configure_file("${PROJECT_SOURCE_DIR}/src/EnergyPlus/api/plugin_tester.py" "${API_TEST_TARGET_DIR}/plugin_tester.py")
add_test(
NAME "API.PluginTesterTester"
COMMAND "${PYTHON_EXECUTABLE}" "${API_TEST_TARGET_DIR}/plugin_tester.py" "${PROJECT_SOURCE_DIR}/testfiles/PythonPluginCustomTrendVariable.py"
COMMAND "${Python_EXECUTABLE}" "${API_TEST_TARGET_DIR}/plugin_tester.py" "${PROJECT_SOURCE_DIR}/testfiles/PythonPluginCustomTrendVariable.py"
WORKING_DIRECTORY "${API_TEST_TARGET_DIR}")

configure_file("${PROJECT_SOURCE_DIR}/tst/EnergyPlus/api/test_DataTransferTypes.py" "${API_TEST_TARGET_DIR}/test_DataTransferTypes.py" COPYONLY)
add_test(
NAME "API.test_DataTransferTypes"
COMMAND "${PYTHON_EXECUTABLE}" -m unittest "${API_TEST_TARGET_DIR}/test_DataTransferTypes.py"
COMMAND "${Python_EXECUTABLE}" -m unittest "${API_TEST_TARGET_DIR}/test_DataTransferTypes.py"
WORKING_DIRECTORY "${API_TEST_TARGET_DIR}")

configure_file("${PROJECT_SOURCE_DIR}/tst/EnergyPlus/api/test_SysExit.py" "${API_TEST_TARGET_DIR}/test_SysExit.py" COPYONLY)
add_test(
NAME "API.test_SysExit"
COMMAND "${PYTHON_EXECUTABLE}" -m unittest "${API_TEST_TARGET_DIR}/test_SysExit.py"
COMMAND "${Python_EXECUTABLE}" -m unittest "${API_TEST_TARGET_DIR}/test_SysExit.py"
WORKING_DIRECTORY "${API_TEST_TARGET_DIR}")

configure_file("${PROJECT_SOURCE_DIR}/tst/EnergyPlus/api/test_OutputFiles.py" "${API_TEST_TARGET_DIR}/test_OutputFiles.py" @ONLY)
add_test(
NAME "API.test_OutputFiles"
COMMAND "${PYTHON_EXECUTABLE}" -m unittest "${API_TEST_TARGET_DIR}/test_OutputFiles.py"
COMMAND "${Python_EXECUTABLE}" -m unittest "${API_TEST_TARGET_DIR}/test_OutputFiles.py"
WORKING_DIRECTORY "${API_TEST_TARGET_DIR}")

endif()
Expand Down
4 changes: 2 additions & 2 deletions tst/EnergyPlus/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,11 @@ set(test_dependencies energyplusapi)
if(LINK_WITH_PYTHON)
add_definitions("-DLINK_WITH_PYTHON")
find_package(PythonLibs 3 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
include_directories(${Python_INCLUDE_DIRS})
if(CMAKE_HOST_UNIX)
list(APPEND test_src api/datatransfer.unit.cc)
list(APPEND test_src PluginManager.unit.cc)
list(APPEND test_dependencies ${PYTHON_LIBRARIES})
list(APPEND test_dependencies ${Python_LIBRARIES})
endif()
endif()

Expand Down

0 comments on commit 4708fe7

Please # to comment.