diff options
author | Eli Schwartz <eschwartz@archlinux.org> | 2021-10-31 20:23:23 -0400 |
---|---|---|
committer | Eli Schwartz <eschwartz@archlinux.org> | 2022-01-10 18:36:57 -0500 |
commit | 140097faf0eddcc7819a3353eb7c21b82a7df1e0 (patch) | |
tree | b70acd7f70f5431a06db73f3db23f86ff896c3b3 /mesonbuild | |
parent | be6e09bfdb5c911eecbc976ed71d427f052a1b81 (diff) | |
download | meson-140097faf0eddcc7819a3353eb7c21b82a7df1e0.zip meson-140097faf0eddcc7819a3353eb7c21b82a7df1e0.tar.gz meson-140097faf0eddcc7819a3353eb7c21b82a7df1e0.tar.bz2 |
port from embedded data to importlib.resources
Diffstat (limited to 'mesonbuild')
-rw-r--r-- | mesonbuild/cmake/data/__init__.py | 0 | ||||
-rw-r--r-- | mesonbuild/cmake/interpreter.py | 4 | ||||
-rw-r--r-- | mesonbuild/dependencies/cmake.py | 4 | ||||
-rw-r--r-- | mesonbuild/dependencies/data/__init__.py | 0 | ||||
-rw-r--r-- | mesonbuild/mesondata.py | 381 |
5 files changed, 21 insertions, 368 deletions
diff --git a/mesonbuild/cmake/data/__init__.py b/mesonbuild/cmake/data/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/mesonbuild/cmake/data/__init__.py diff --git a/mesonbuild/cmake/interpreter.py b/mesonbuild/cmake/interpreter.py index c2e2d27..0b6d241 100644 --- a/mesonbuild/cmake/interpreter.py +++ b/mesonbuild/cmake/interpreter.py @@ -24,7 +24,7 @@ from .traceparser import CMakeTraceParser, CMakeGeneratorTarget from .tracetargets import resolve_cmake_trace_targets from .. import mlog, mesonlib from ..mesonlib import MachineChoice, OrderedSet, version_compare, path_is_in_root, relative_to_if_possible, OptionKey -from ..mesondata import mesondata +from ..mesondata import DataFile from ..compilers.compilers import assembler_suffixes, lang_suffixes, header_suffixes, obj_suffixes, lib_suffixes, is_header from ..programs import ExternalProgram from ..coredata import FORBIDDEN_TARGET_NAMES @@ -816,7 +816,7 @@ class CMakeInterpreter: raise CMakeException('Unable to find CMake') self.trace = CMakeTraceParser(cmake_exe.version(), self.build_dir, permissive=True) - preload_file = mesondata['cmake/data/preload.cmake'].write_to_private(self.env) + preload_file = DataFile('cmake/data/preload.cmake').write_to_private(self.env) toolchain = CMakeToolchain(cmake_exe, self.env, self.for_machine, CMakeExecScope.SUBPROJECT, self.build_dir, preload_file) toolchain_file = toolchain.write() diff --git a/mesonbuild/dependencies/cmake.py b/mesonbuild/dependencies/cmake.py index a6898f8..dcbe5df 100644 --- a/mesonbuild/dependencies/cmake.py +++ b/mesonbuild/dependencies/cmake.py @@ -14,9 +14,9 @@ from .base import ExternalDependency, DependencyException, DependencyTypeName from ..mesonlib import is_windows, MesonException, OptionKey, PerMachine, stringlistify, extract_as_list -from ..mesondata import mesondata from ..cmake import CMakeExecutor, CMakeTraceParser, CMakeException, CMakeToolchain, CMakeExecScope, check_cmake_args, CMakeTarget, resolve_cmake_trace_targets, cmake_is_debug from .. import mlog +import importlib.resources from pathlib import Path import functools import re @@ -583,7 +583,7 @@ class CMakeDependency(ExternalDependency): shutil.rmtree(cmake_files.as_posix(), ignore_errors=True) # Insert language parameters into the CMakeLists.txt and write new CMakeLists.txt - cmake_txt = mesondata['dependencies/data/' + cmake_file].data + cmake_txt = importlib.resources.read_text('mesonbuild.dependencies.data', cmake_file, encoding = 'utf-8') # In general, some Fortran CMake find_package() also require C language enabled, # even if nothing from C is directly used. An easy Fortran example that fails diff --git a/mesonbuild/dependencies/data/__init__.py b/mesonbuild/dependencies/data/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/mesonbuild/dependencies/data/__init__.py diff --git a/mesonbuild/mesondata.py b/mesonbuild/mesondata.py index 43b7bde..508c041 100644 --- a/mesonbuild/mesondata.py +++ b/mesonbuild/mesondata.py @@ -13,382 +13,35 @@ # limitations under the License. -#### -#### WARNING: This is an automatically generated file! Do not edit! -#### Generated by tools/gen_data.py -#### - - -# TODO: Remember to remove this also from tools/gen_data.py -from pathlib import Path +import importlib.resources +from pathlib import PurePosixPath, Path import typing as T if T.TYPE_CHECKING: from .environment import Environment -###################### -# BEGIN Data section # -###################### - -file_0_data_preload_cmake = '''\ -if(MESON_PS_LOADED) - return() -endif() - -set(MESON_PS_LOADED ON) - -cmake_policy(PUSH) -cmake_policy(SET CMP0054 NEW) # https://cmake.org/cmake/help/latest/policy/CMP0054.html - -# Dummy macros that have a special meaning in the meson code -macro(meson_ps_execute_delayed_calls) -endmacro() - -macro(meson_ps_reload_vars) -endmacro() - -macro(meson_ps_disabled_function) - message(WARNING "The function '${ARGV0}' is disabled in the context of CMake subprojects.\n" - "This should not be an issue but may lead to compilation errors.") -endmacro() - -# Helper macro to inspect the current CMake state -macro(meson_ps_inspect_vars) - set(MESON_PS_CMAKE_CURRENT_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}") - set(MESON_PS_CMAKE_CURRENT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") - meson_ps_execute_delayed_calls() -endmacro() - - -# Override some system functions with custom code and forward the args -# to the original function -macro(add_custom_command) - meson_ps_inspect_vars() - _add_custom_command(${ARGV}) -endmacro() - -macro(add_custom_target) - meson_ps_inspect_vars() - _add_custom_target(${ARGV}) -endmacro() - -macro(set_property) - meson_ps_inspect_vars() - _set_property(${ARGV}) -endmacro() - -function(set_source_files_properties) - set(FILES) - set(I 0) - set(PROPERTIES OFF) - - while(I LESS ARGC) - if(NOT PROPERTIES) - if("${ARGV${I}}" STREQUAL "PROPERTIES") - set(PROPERTIES ON) - else() - list(APPEND FILES "${ARGV${I}}") - endif() - - math(EXPR I "${I} + 1") - else() - set(ID_IDX ${I}) - math(EXPR PROP_IDX "${ID_IDX} + 1") - - set(ID "${ARGV${ID_IDX}}") - set(PROP "${ARGV${PROP_IDX}}") - - set_property(SOURCE ${FILES} PROPERTY "${ID}" "${PROP}") - math(EXPR I "${I} + 2") - endif() - endwhile() -endfunction() - -# Disable some functions that would mess up the CMake meson integration -macro(target_precompile_headers) - meson_ps_disabled_function(target_precompile_headers) -endmacro() - -set(MESON_PS_DELAYED_CALLS add_custom_command;add_custom_target;set_property) -meson_ps_reload_vars() - -cmake_policy(POP) -''' - -file_1_data_CMakeLists_txt = '''\ -# fail noisily if attempt to use this file without setting: -# cmake_minimum_required(VERSION ${CMAKE_VERSION}) -# project(... LANGUAGES ...) - -cmake_policy(SET CMP0000 NEW) - -set(PACKAGE_FOUND FALSE) -set(_packageName "${NAME}") -string(TOUPPER "${_packageName}" PACKAGE_NAME) - -while(TRUE) - if ("${VERSION}" STREQUAL "") - find_package("${NAME}" QUIET COMPONENTS ${COMPS}) - else() - find_package("${NAME}" "${VERSION}" QUIET COMPONENTS ${COMPS}) - endif() - - # ARCHS has to be set via the CMD interface - if(${_packageName}_FOUND OR ${PACKAGE_NAME}_FOUND OR "${ARCHS}" STREQUAL "") - break() - endif() - - list(GET ARCHS 0 CMAKE_LIBRARY_ARCHITECTURE) - list(REMOVE_AT ARCHS 0) -endwhile() - -if(${_packageName}_FOUND OR ${PACKAGE_NAME}_FOUND) - set(PACKAGE_FOUND TRUE) - - # Check the following variables: - # FOO_VERSION - # Foo_VERSION - # FOO_VERSION_STRING - # Foo_VERSION_STRING - if(NOT DEFINED PACKAGE_VERSION) - if(DEFINED ${_packageName}_VERSION) - set(PACKAGE_VERSION "${${_packageName}_VERSION}") - elseif(DEFINED ${PACKAGE_NAME}_VERSION) - set(PACKAGE_VERSION "${${PACKAGE_NAME}_VERSION}") - elseif(DEFINED ${_packageName}_VERSION_STRING) - set(PACKAGE_VERSION "${${_packageName}_VERSION_STRING}") - elseif(DEFINED ${PACKAGE_NAME}_VERSION_STRING) - set(PACKAGE_VERSION "${${PACKAGE_NAME}_VERSION_STRING}") - endif() - endif() - - # Check the following variables: - # FOO_LIBRARIES - # Foo_LIBRARIES - # FOO_LIBS - # Foo_LIBS - set(libs) - if(DEFINED ${_packageName}_LIBRARIES) - set(libs ${_packageName}_LIBRARIES) - elseif(DEFINED ${PACKAGE_NAME}_LIBRARIES) - set(libs ${PACKAGE_NAME}_LIBRARIES) - elseif(DEFINED ${_packageName}_LIBS) - set(libs ${_packageName}_LIBS) - elseif(DEFINED ${PACKAGE_NAME}_LIBS) - set(libs ${PACKAGE_NAME}_LIBS) - endif() - - # Check the following variables: - # FOO_INCLUDE_DIRS - # Foo_INCLUDE_DIRS - # FOO_INCLUDES - # Foo_INCLUDES - # FOO_INCLUDE_DIR - # Foo_INCLUDE_DIR - set(includes) - if(DEFINED ${_packageName}_INCLUDE_DIRS) - set(includes ${_packageName}_INCLUDE_DIRS) - elseif(DEFINED ${PACKAGE_NAME}_INCLUDE_DIRS) - set(includes ${PACKAGE_NAME}_INCLUDE_DIRS) - elseif(DEFINED ${_packageName}_INCLUDES) - set(includes ${_packageName}_INCLUDES) - elseif(DEFINED ${PACKAGE_NAME}_INCLUDES) - set(includes ${PACKAGE_NAME}_INCLUDES) - elseif(DEFINED ${_packageName}_INCLUDE_DIR) - set(includes ${_packageName}_INCLUDE_DIR) - elseif(DEFINED ${PACKAGE_NAME}_INCLUDE_DIR) - set(includes ${PACKAGE_NAME}_INCLUDE_DIR) - endif() - - # Check the following variables: - # FOO_DEFINITIONS - # Foo_DEFINITIONS - set(definitions) - if(DEFINED ${_packageName}_DEFINITIONS) - set(definitions ${_packageName}_DEFINITIONS) - elseif(DEFINED ${PACKAGE_NAME}_DEFINITIONS) - set(definitions ${PACKAGE_NAME}_DEFINITIONS) - endif() - - set(PACKAGE_INCLUDE_DIRS "${${includes}}") - set(PACKAGE_DEFINITIONS "${${definitions}}") - set(PACKAGE_LIBRARIES "${${libs}}") -endif() -''' - -file_2_data_CMakeListsLLVM_txt = '''\ -cmake_minimum_required(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION} ) - -set(PACKAGE_FOUND FALSE) - -while(TRUE) - find_package(LLVM REQUIRED CONFIG QUIET) - - # ARCHS has to be set via the CMD interface - if(LLVM_FOUND OR "${ARCHS}" STREQUAL "") - break() - endif() - - list(GET ARCHS 0 CMAKE_LIBRARY_ARCHITECTURE) - list(REMOVE_AT ARCHS 0) -endwhile() - -if(LLVM_FOUND) - set(PACKAGE_FOUND TRUE) - - foreach(mod IN LISTS LLVM_MESON_MODULES) - # Reset variables - set(out_mods) - set(real_mods) - - # Generate a lower and upper case version - string(TOLOWER "${mod}" mod_L) - string(TOUPPER "${mod}" mod_U) - - # Get the mapped components - llvm_map_components_to_libnames(out_mods ${mod} ${mod_L} ${mod_U}) - list(SORT out_mods) - list(REMOVE_DUPLICATES out_mods) - - # Make sure that the modules exist - foreach(i IN LISTS out_mods) - if(TARGET ${i}) - list(APPEND real_mods ${i}) - endif() - endforeach() - - # Set the output variables - set(MESON_LLVM_TARGETS_${mod} ${real_mods}) - foreach(i IN LISTS real_mods) - set(MESON_TARGET_TO_LLVM_${i} ${mod}) - endforeach() - endforeach() - - # Check the following variables: - # LLVM_PACKAGE_VERSION - # LLVM_VERSION - # LLVM_VERSION_STRING - if(NOT DEFINED PACKAGE_VERSION) - if(DEFINED LLVM_PACKAGE_VERSION) - set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}") - elseif(DEFINED LLVM_VERSION) - set(PACKAGE_VERSION "${LLVM_VERSION}") - elseif(DEFINED LLVM_VERSION_STRING) - set(PACKAGE_VERSION "${LLVM_VERSION_STRING}") - endif() - endif() - - # Check the following variables: - # LLVM_LIBRARIES - # LLVM_LIBS - set(libs) - if(DEFINED LLVM_LIBRARIES) - set(libs LLVM_LIBRARIES) - elseif(DEFINED LLVM_LIBS) - set(libs LLVM_LIBS) - endif() - - # Check the following variables: - # LLVM_INCLUDE_DIRS - # LLVM_INCLUDES - # LLVM_INCLUDE_DIR - set(includes) - if(DEFINED LLVM_INCLUDE_DIRS) - set(includes LLVM_INCLUDE_DIRS) - elseif(DEFINED LLVM_INCLUDES) - set(includes LLVM_INCLUDES) - elseif(DEFINED LLVM_INCLUDE_DIR) - set(includes LLVM_INCLUDE_DIR) - endif() - - # Check the following variables: - # LLVM_DEFINITIONS - set(definitions) - if(DEFINED LLVM_DEFINITIONS) - set(definitions LLVM_DEFINITIONS) - endif() - - set(PACKAGE_INCLUDE_DIRS "${${includes}}") - set(PACKAGE_DEFINITIONS "${${definitions}}") - set(PACKAGE_LIBRARIES "${${libs}}") -endif() -''' - -file_3_data_CMakePathInfo_txt = '''\ -cmake_minimum_required(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION}) - -set(TMP_PATHS_LIST) -list(APPEND TMP_PATHS_LIST ${CMAKE_PREFIX_PATH}) -list(APPEND TMP_PATHS_LIST ${CMAKE_FRAMEWORK_PATH}) -list(APPEND TMP_PATHS_LIST ${CMAKE_APPBUNDLE_PATH}) -list(APPEND TMP_PATHS_LIST $ENV{CMAKE_PREFIX_PATH}) -list(APPEND TMP_PATHS_LIST $ENV{CMAKE_FRAMEWORK_PATH}) -list(APPEND TMP_PATHS_LIST $ENV{CMAKE_APPBUNDLE_PATH}) -list(APPEND TMP_PATHS_LIST ${CMAKE_SYSTEM_PREFIX_PATH}) -list(APPEND TMP_PATHS_LIST ${CMAKE_SYSTEM_FRAMEWORK_PATH}) -list(APPEND TMP_PATHS_LIST ${CMAKE_SYSTEM_APPBUNDLE_PATH}) - -set(LIB_ARCH_LIST) -if(CMAKE_LIBRARY_ARCHITECTURE_REGEX) - file(GLOB implicit_dirs RELATIVE /lib /lib/*-linux-gnu* ) - foreach(dir ${implicit_dirs}) - if("${dir}" MATCHES "${CMAKE_LIBRARY_ARCHITECTURE_REGEX}") - list(APPEND LIB_ARCH_LIST "${dir}") - endif() - endforeach() -endif() - -# "Export" these variables: -set(MESON_ARCH_LIST ${LIB_ARCH_LIST}) -set(MESON_PATHS_LIST ${TMP_PATHS_LIST}) -set(MESON_CMAKE_ROOT ${CMAKE_ROOT}) -set(MESON_CMAKE_SYSROOT ${CMAKE_SYSROOT}) -set(MESON_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH}) - -message(STATUS ${TMP_PATHS_LIST}) -''' - - -#################### -# END Data section # -#################### - class DataFile: - def __init__(self, path: Path, sha256sum: str, data: str) -> None: - self.path = path - self.sha256sum = sha256sum - self.data = data + def __init__(self, path: str) -> None: + self.path = PurePosixPath(path) def write_once(self, path: Path) -> None: if not path.exists(): - path.write_text(self.data, encoding='utf-8') + data = importlib.resources.read_text( # [ignore encoding] it's on the next lines, Mr. Lint + ('mesonbuild' / self.path.parent).as_posix().replace('/', '.'), + self.path.name, + encoding='utf-8') + path.write_text(data, encoding='utf-8') def write_to_private(self, env: 'Environment') -> Path: + try: + resource = importlib.resources.files('mesonbuild') / self.path + if isinstance(resource, Path): + return resource + except AttributeError: + # fall through to python 3.7 compatible code + pass + out_file = Path(env.scratch_dir) / 'data' / self.path.name out_file.parent.mkdir(exist_ok=True) self.write_once(out_file) return out_file - - -mesondata = { - 'cmake/data/preload.cmake': DataFile( - Path('cmake/data/preload.cmake'), - 'ce8f30159aab25b92c26c58a219a427d47838bfa0739475221d6c8993b4946e5', - file_0_data_preload_cmake, - ), - 'dependencies/data/CMakeLists.txt': DataFile( - Path('dependencies/data/CMakeLists.txt'), - '4dca24afa13e9311f0598a6ac29690490819bd7d82cfdaa0a2fe5eea3c0fa0d5', - file_1_data_CMakeLists_txt, - ), - 'dependencies/data/CMakeListsLLVM.txt': DataFile( - Path('dependencies/data/CMakeListsLLVM.txt'), - '412cec3315597041a978d018cdaca282dcd47693793540da88ae2f80d0cbd7cd', - file_2_data_CMakeListsLLVM_txt, - ), - 'dependencies/data/CMakePathInfo.txt': DataFile( - Path('dependencies/data/CMakePathInfo.txt'), - '90da8b443982d9c87139b7dc84228eb58cab4315764949637208f25e2bda7db2', - file_3_data_CMakePathInfo_txt, - ), -} |