Skip to content

Commit

Permalink
Merge pull request mesonbuild#5749 from mensinda/cmGenExp
Browse files Browse the repository at this point in the history
CMake: Basic generator expression support
  • Loading branch information
jpakkane authored Aug 2, 2019
2 parents 9a62d69 + 07f4885 commit cba2341
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 3 deletions.
2 changes: 2 additions & 0 deletions mesonbuild/cmake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
'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
129 changes: 129 additions & 0 deletions mesonbuild/cmake/generator.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions mesonbuild/cmake/traceparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -448,7 +449,6 @@ def _lex_trace(self, trace):
# The trace format is: '<file>(<line>): <func>(<args -- can contain \n> )\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)
Expand All @@ -466,9 +466,10 @@ def _lex_trace(self, trace):
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)

Expand Down
10 changes: 10 additions & 0 deletions test cases/cmake/10 generator expressions/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>
#include <cmMod.hpp>

using namespace std;

int main() {
cmModClass obj("Hello");
cout << obj.getStr() << endl;
return 0;
}
12 changes: 12 additions & 0 deletions test cases/cmake/10 generator expressions/meson.build
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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 $<$<AND:$<CONFIG:Release>,$<CONFIG:Debug>>:-DCMAKE_FLAG_ERROR_A> # Check discard = false
INTERFACE "-DCMAKE_FLAG_REQUIRED_A"
INTERFACE $<$<AND:1,$<STREQUAL:asd,$<LOWER_CASE:AsD>>,$<NOT:$<EQUAL:4,2>>>:-DCMAKE_FLAG_REQUIRED_B>
INTERFACE $<$<VERSION_LESS:1.2.3,2.1.0>:-DCMAKE_FLAG_REQUIRED_C>
)

target_include_directories(cmModLib INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

target_compile_definitions(cmModLib INTERFACE -DCMAKE_COMPILER_DEFINE_STR="compDef")
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <string>

#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; }
};

0 comments on commit cba2341

Please sign in to comment.