CMake Build: Fix missing pythonXY.zip on MSVC

Ammends 94b9b33a17

Change-Id: I531a648a108233fbefbe6878f65f8ab7186db39a
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Cristian Adam
2020-10-09 14:17:59 +02:00
parent 3ff8c42431
commit d7e24f28ba
2 changed files with 100 additions and 6 deletions

View File

@@ -0,0 +1,79 @@
# create_python_xy function will precompile the Python/lib/*.py files
# and create a zip file containing all the pyc files
function(create_python_xy PythonExe PythonZipFilePath)
get_filename_component(python_lib_dir "${PythonExe}" DIRECTORY)
get_filename_component(python_lib_dir "${python_lib_dir}/Lib" ABSOLUTE)
foreach(dir collections encodings importlib json urllib)
file(COPY ${python_lib_dir}/${dir}
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/python-lib
FILES_MATCHING PATTERN "*.py"
)
endforeach()
file(GLOB python_lib_files "${python_lib_dir}/*.py")
foreach(not_needed
aifc.py imghdr.py socket.py
antigravity.py imp.py socketserver.py
argparse.py ipaddress.py ssl.py
asynchat.py locale.py statistics.py
asyncore.py lzma.py string.py
bdb.py mailbox.py stringprep.py
binhex.py mailcap.py sunau.py
bisect.py mimetypes.py symbol.py
bz2.py modulefinder.py symtable.py
calendar.py netrc.py tabnanny.py
cgi.py nntplib.py tarfile.py
cgitb.py nturl2path.py telnetlib.py
chunk.py numbers.py tempfile.py
cmd.py optparse.py textwrap.py
code.py pathlib.py this.py
codeop.py pdb.py timeit.py
colorsys.py pickle.py trace.py
compileall.py pickletools.py tracemalloc.py
configparser.py pipes.py tty.py
contextvars.py plistlib.py turtle.py
cProfile.py poplib.py typing.py
crypt.py pprint.py uu.py
csv.py profile.py uuid.py
dataclasses.py pstats.py wave.py
datetime.py pty.py webbrowser.py
decimal.py pyclbr.py xdrlib.py
difflib.py py_compile.py zipapp.py
doctest.py queue.py zipfile.py
dummy_threading.py quopri.py zipimport.py
filecmp.py random.py _compat_pickle.py
fileinput.py rlcompleter.py _compression.py
formatter.py runpy.py _dummy_thread.py
fractions.py sched.py _markupbase.py
ftplib.py secrets.py _osx_support.py
getopt.py selectors.py _pydecimal.py
getpass.py shelve.py _pyio.py
gettext.py shlex.py _py_abc.py
gzip.py shutil.py _strptime.py
hashlib.py smtpd.py _threading_local.py
hmac.py smtplib.py __future__.py
imaplib.py sndhdr.py __phello__.foo.py
)
list(FIND python_lib_files "${python_lib_dir}/${not_needed}" found_not_needed)
if (NOT found_not_needed STREQUAL "-1")
list(REMOVE_AT python_lib_files ${found_not_needed})
endif()
endforeach()
file(COPY ${python_lib_files} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/python-lib")
set(ENV{PYTHONOPTIMIZE} "2")
execute_process(
COMMAND "${PythonExe}" -OO -m compileall "${CMAKE_CURRENT_BINARY_DIR}/python-lib" -b
)
file(GLOB_RECURSE python_lib_files "${CMAKE_CURRENT_BINARY_DIR}/python-lib/*.py")
file(REMOVE ${python_lib_files})
file(GLOB_RECURSE python_lib_files LIST_DIRECTORIES ON "${CMAKE_CURRENT_BINARY_DIR}/python-lib/*/__pycache__$")
file(REMOVE_RECURSE ${python_lib_files})
execute_process(
COMMAND ${CMAKE_COMMAND} -E tar cf "${PythonZipFilePath}" . --format=zip
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/python-lib/"
)
endfunction()

View File

@@ -59,8 +59,12 @@ if (_library_enabled)
foreach(lib IN LISTS PYTHON_LIBRARIES) foreach(lib IN LISTS PYTHON_LIBRARIES)
if (lib MATCHES ${PythonRegex}) if (lib MATCHES ${PythonRegex})
set(PythonZipFileName "python${CMAKE_MATCH_4}.zip")
set(PythonDll "${CMAKE_MATCH_1}/${CMAKE_MATCH_3}${CMAKE_SHARED_LIBRARY_SUFFIX}") set(PythonDll "${CMAKE_MATCH_1}/${CMAKE_MATCH_3}${CMAKE_SHARED_LIBRARY_SUFFIX}")
set(PythonZip "${CMAKE_MATCH_1}/python${CMAKE_MATCH_4}.zip") set(PythonExe "${CMAKE_MATCH_1}/python${CMAKE_EXECUTABLE_SUFFIX}")
set(PythonZip "${CMAKE_MATCH_1}/${PythonZipFileName}")
break() break()
endif() endif()
endforeach() endforeach()
@@ -85,16 +89,27 @@ if (_library_enabled)
pyvalue.cpp pyvalue.h pyvalue.cpp pyvalue.h
) )
install(FILES if (NOT EXISTS "${PythonZip}" AND
"${PythonDll}" NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/${PythonZipFileName}")
"${PythonZip}" include(CreatePythonXY)
create_python_xy("${PythonExe}" "${CMAKE_CURRENT_BINARY_DIR}/${PythonZipFileName}")
endif()
if (NOT EXISTS "${PythonZip}" AND
EXISTS "${CMAKE_CURRENT_BINARY_DIR}/${PythonZipFileName}")
set(PythonZip "${CMAKE_CURRENT_BINARY_DIR}/${PythonZipFileName}")
endif()
list(APPEND deployPythonFiles "${PythonDll}")
list(APPEND deployPythonFiles "${PythonZip}")
install(FILES ${deployPythonFiles}
DESTINATION lib/qtcreatorcdbext${ArchSuffix}/ DESTINATION lib/qtcreatorcdbext${ArchSuffix}/
COMPONENT qtcreatorcdbext) COMPONENT qtcreatorcdbext)
add_custom_target(copy_python_dll ALL VERBATIM) add_custom_target(copy_python_dll ALL VERBATIM)
add_custom_command(TARGET copy_python_dll POST_BUILD add_custom_command(TARGET copy_python_dll POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy "${PythonDll}" "${PROJECT_BINARY_DIR}/lib/qtcreatorcdbext${ArchSuffix}/" COMMAND "${CMAKE_COMMAND}" -E copy ${deployPythonFiles} "${PROJECT_BINARY_DIR}/lib/qtcreatorcdbext${ArchSuffix}/"
COMMAND "${CMAKE_COMMAND}" -E copy "${PythonZip}" "${PROJECT_BINARY_DIR}/lib/qtcreatorcdbext${ArchSuffix}/"
VERBATIM VERBATIM
) )
endif() endif()