From bee74eea16b4f7849c154706398224c9a542c4bd Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Thu, 11 Jul 2019 22:10:38 +0200 Subject: cmake: Parse a subset of generator expressions --- mesonbuild/cmake/__init__.py | 2 + mesonbuild/cmake/generator.py | 129 ++++++++++++++++++++++++++++++++++++++++ mesonbuild/cmake/traceparser.py | 7 ++- 3 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 mesonbuild/cmake/generator.py diff --git a/mesonbuild/cmake/__init__.py b/mesonbuild/cmake/__init__.py index 01e1980..f9835a1 100644 --- a/mesonbuild/cmake/__init__.py +++ b/mesonbuild/cmake/__init__.py @@ -23,10 +23,12 @@ __all__ = [ 'CMakeTarget', 'CMakeTraceLine', 'CMakeTraceParser', + 'parse_generator_expressions', ] from .common import CMakeException from .client import CMakeClient from .executor import CMakeExecutor +from .generator import parse_generator_expressions from .interpreter import CMakeInterpreter from .traceparser import CMakeTarget, CMakeTraceLine, CMakeTraceParser diff --git a/mesonbuild/cmake/generator.py b/mesonbuild/cmake/generator.py new file mode 100644 index 0000000..a30d2de --- /dev/null +++ b/mesonbuild/cmake/generator.py @@ -0,0 +1,129 @@ +# Copyright 2019 The Meson development team + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from .. import mesonlib + +def parse_generator_expressions(raw: str) -> str: + '''Parse CMake generator expressions + + Most generator expressions are simply ignored for + simplicety, however some are required for some common + use cases. + ''' + + out = '' # type: str + i = 0 # type: int + + def equal(arg: str) -> str: + col_pos = arg.find(',') + if col_pos < 0: + return '0' + else: + return '1' if arg[:col_pos] == arg[col_pos + 1:] else '0' + + def vers_comp(op: str, arg: str) -> str: + col_pos = arg.find(',') + if col_pos < 0: + return '0' + else: + return '1' if mesonlib.version_compare(arg[:col_pos], '{}{}'.format(op, arg[col_pos + 1:])) else '0' + + supported = { + # Boolean functions + 'BOOL': lambda x: '0' if x.upper() in ['0', 'FALSE', 'OFF', 'N', 'NO', 'IGNORE', 'NOTFOUND'] or x.endswith('-NOTFOUND') else '1', + 'AND': lambda x: '1' if all([y == '1' for y in x.split(',')]) else '0', + 'OR': lambda x: '1' if any([y == '1' for y in x.split(',')]) else '0', + 'NOT': lambda x: '0' if x == '1' else '1', + + '0': lambda x: '', + '1': lambda x: x, + + # String operations + 'STREQUAL': equal, + 'EQUAL': equal, + 'VERSION_LESS': lambda x: vers_comp('<', x), + 'VERSION_GREATER': lambda x: vers_comp('>', x), + 'VERSION_EQUAL': lambda x: vers_comp('=', x), + 'VERSION_LESS_EQUAL': lambda x: vers_comp('<=', x), + 'VERSION_GREATER_EQUAL': lambda x: vers_comp('>=', x), + + # String modification + 'LOWER_CASE': lambda x: x.lower(), + 'UPPER_CASE': lambda x: x.upper(), + + # Always assume the BUILD_INTERFACE is valid. + # INSTALL_INTERFACE is always invalid for subprojects and + # it should also never appear in CMake config files, used + # for dependencies + 'INSTALL_INTERFACE': lambda x: '', + 'BUILD_INTERFACE': lambda x: x, + + # Constants + 'ANGLE-R': lambda x: '>', + 'COMMA': lambda x: ',', + 'SEMICOLON': lambda x: ';', + } + + # Recursively evaluate generator expressions + def eval_generator_expressions() -> str: + nonlocal i + i += 2 + + func = '' # type: str + args = '' # type: str + res = '' # type: str + exp = '' # type: str + + # Determine the body of the expression + while i < len(raw): + if raw[i] == '>': + # End of the generator expression + break + elif i < len(raw) - 1 and raw[i] == '$' and raw[i + 1] == '<': + # Nested generator expression + exp += eval_generator_expressions() + else: + # Generator expression body + exp += raw[i] + + i += 1 + + # Split the expression into a function and arguments part + col_pos = exp.find(':') + if col_pos < 0: + func = exp + else: + func = exp[:col_pos] + args = exp[col_pos + 1:] + + func = func.strip() + args = args.strip() + + # Evaluate the function + if func in supported: + res = supported[func](args) + + return res + + while i < len(raw): + if i < len(raw) - 1 and raw[i] == '$' and raw[i + 1] == '<': + # Generator expression detected --> try resolving it + out += eval_generator_expressions() + else: + # Normal string, leave unchanged + out += raw[i] + + i += 1 + + return out diff --git a/mesonbuild/cmake/traceparser.py b/mesonbuild/cmake/traceparser.py index 6106d16..3a3f269 100644 --- a/mesonbuild/cmake/traceparser.py +++ b/mesonbuild/cmake/traceparser.py @@ -16,6 +16,7 @@ # or an interpreter-based tool. from .common import CMakeException +from .generator import parse_generator_expressions from .. import mlog from typing import List, Tuple, Optional @@ -448,7 +449,6 @@ class CMakeTraceParser: # The trace format is: '(): ( )\n' reg_tline = re.compile(r'\s*(.*\.(cmake|txt))\(([0-9]+)\):\s*(\w+)\(([\s\S]*?) ?\)\s*\n', re.MULTILINE) reg_other = re.compile(r'[^\n]*\n') - reg_genexp = re.compile(r'\$<.*>') loc = 0 while loc < len(trace): mo_file_line = reg_tline.match(trace, loc) @@ -466,9 +466,10 @@ class CMakeTraceParser: file = mo_file_line.group(1) line = mo_file_line.group(3) func = mo_file_line.group(4) - args = mo_file_line.group(5).split(' ') + args = mo_file_line.group(5) + args = parse_generator_expressions(args) + args = args.split(' ') args = list(map(lambda x: x.strip(), args)) - args = list(map(lambda x: reg_genexp.sub('', x), args)) # Remove generator expressions yield CMakeTraceLine(file, line, func, args) -- cgit v1.1 From 07f48851d0439471b50ffd09d2bb89f548ac3ae0 Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Thu, 11 Jul 2019 22:11:09 +0200 Subject: cmake: added generator expression test case --- test cases/cmake/10 generator expressions/main.cpp | 10 +++++++ .../cmake/10 generator expressions/meson.build | 12 +++++++++ .../subprojects/cmMod/CMakeLists.txt | 22 +++++++++++++++ .../subprojects/cmMod/include/cmMod.hpp | 31 ++++++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 test cases/cmake/10 generator expressions/main.cpp create mode 100644 test cases/cmake/10 generator expressions/meson.build create mode 100644 test cases/cmake/10 generator expressions/subprojects/cmMod/CMakeLists.txt create mode 100644 test cases/cmake/10 generator expressions/subprojects/cmMod/include/cmMod.hpp diff --git a/test cases/cmake/10 generator expressions/main.cpp b/test cases/cmake/10 generator expressions/main.cpp new file mode 100644 index 0000000..315c0f7 --- /dev/null +++ b/test cases/cmake/10 generator expressions/main.cpp @@ -0,0 +1,10 @@ +#include +#include + +using namespace std; + +int main() { + cmModClass obj("Hello"); + cout << obj.getStr() << endl; + return 0; +} diff --git a/test cases/cmake/10 generator expressions/meson.build b/test cases/cmake/10 generator expressions/meson.build new file mode 100644 index 0000000..ca08a3f --- /dev/null +++ b/test cases/cmake/10 generator expressions/meson.build @@ -0,0 +1,12 @@ +project('cmakeSubTest', ['c', 'cpp']) + +cm = import('cmake') + +sub_pro = cm.subproject('cmMod') +sub_dep = sub_pro.dependency('cmModLib') + +assert(sub_pro.target_list() == ['cmModLib'], 'There should be exactly one target') +assert(sub_pro.target_type('cmModLib') == 'header_only', 'Target type should be header_only') + +exe1 = executable('main', ['main.cpp'], dependencies: [sub_dep]) +test('test1', exe1) diff --git a/test cases/cmake/10 generator expressions/subprojects/cmMod/CMakeLists.txt b/test cases/cmake/10 generator expressions/subprojects/cmMod/CMakeLists.txt new file mode 100644 index 0000000..dc4f9e4 --- /dev/null +++ b/test cases/cmake/10 generator expressions/subprojects/cmMod/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.5) + +project(cmMod) +set (CMAKE_CXX_STANDARD 14) + +include(GNUInstallDirs) + +add_library(cmModLib INTERFACE) + +target_compile_options(cmModLib + INTERFACE $<$,$>:-DCMAKE_FLAG_ERROR_A> # Check discard = false + INTERFACE "-DCMAKE_FLAG_REQUIRED_A" + INTERFACE $<$>,$>>:-DCMAKE_FLAG_REQUIRED_B> + INTERFACE $<$:-DCMAKE_FLAG_REQUIRED_C> +) + +target_include_directories(cmModLib INTERFACE + $ + $ +) + +target_compile_definitions(cmModLib INTERFACE -DCMAKE_COMPILER_DEFINE_STR="compDef") diff --git a/test cases/cmake/10 generator expressions/subprojects/cmMod/include/cmMod.hpp b/test cases/cmake/10 generator expressions/subprojects/cmMod/include/cmMod.hpp new file mode 100644 index 0000000..1f00107 --- /dev/null +++ b/test cases/cmake/10 generator expressions/subprojects/cmMod/include/cmMod.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include + +#ifndef CMAKE_FLAG_REQUIRED_A +#error "The flag CMAKE_FLAG_REQUIRED_A was not set" +#endif + +#ifndef CMAKE_FLAG_REQUIRED_B +#error "The flag CMAKE_FLAG_REQUIRED_B was not set" +#endif + +#ifndef CMAKE_FLAG_REQUIRED_C +#error "The flag CMAKE_FLAG_REQUIRED_C was not set" +#endif + +#ifdef CMAKE_FLAG_ERROR_A +#error "The flag CMAKE_FLAG_ERROR_A was set" +#endif + +class cmModClass { + private: + std::string str; + public: + cmModClass(std::string foo) { + str = foo + " World "; + str += CMAKE_COMPILER_DEFINE_STR; + } + + inline std::string getStr() const { return str; } +}; -- cgit v1.1