Skip to content

Commit

Permalink
add support for generating cmake files
Browse files Browse the repository at this point in the history
This new cmake module allows to generate cmake package files. This may ease the
porting for cmake projects that are exporting cmake package informations for other
depending projects. The module uses as much as possible the templates provided by
the cmake installation (and so cmake needs to be installed).
  • Loading branch information
hardening committed Feb 19, 2018
1 parent 55a7c26 commit b31c7aa
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 0 deletions.
71 changes: 71 additions & 0 deletions docs/markdown/CMake-module.md
@@ -0,0 +1,71 @@
# CMake module

This module provides helper tools for generating cmake package files.


## Usage

To use this module, just do: **`cmake = import('cmake')`**. The
following functions will then be available as methods on the object
with the name `cmake`. You can, of course, replace the name `cmake`
with anything else.

### cmake.write_basic_package_version_file()

This function is the equivalent of the corresponding [CMake function](https://cmake.org/cmake/help/v3.11/module/CMakePackageConfigHelpers.html#generating-a-package-version-file),
it generates a `name` package version file.

* `name`: the name of the package.
* `version`: the version of the generated package file.
* `compatibility`: a string indicating the kind of compatibility, the accepted values are
`AnyNewerVersion`, `SameMajorVersion`, `SameMinorVersion` or `ExactVersion`.
It defaults to `AnyNewerVersion`. Depending on your cmake installation some kind of
compatibility may not be available.
* `install_dir`: optional installation directory, it defaults to `$(libdir)/cmake/$(name)`


Example:

```meson
cmake = import('cmake')
cmake.write_basic_package_version_file(name: 'myProject', version: '1.0.0')
```

### cmake.configure_package_config_file()

This function is the equivalent of the corresponding [CMake function](https://cmake.org/cmake/help/v3.11/module/CMakePackageConfigHelpers.html#generating-a-package-configuration-file),
it generates a `name` package configuration file from the `input` template file. Just like the cmake function
in this file the `@PACKAGE_INIT@` statement will be replaced by the appropriate piece of cmake code.
The equivalent `PATH_VARS` argument is given through the `configuration` parameter.

* `name`: the name of the package.
* `input`: the template file where that will be treated for variable substitutions contained in `configuration`.
* `install_dir`: optional installation directory, it defaults to `$(libdir)/cmake/$(name)`.
* `configuration`: a `configuration_data` object that will be used for variable substitution in the template file.


Example:

meson.build:

```meson
cmake = import('cmake')
conf = configuration_data()
conf.set_quoted('VAR', 'variable value')
cmake.configure_package_config_file(
name: 'myProject',
input: 'myProject.cmake.in',
configuration: conf
)
```

myProject.cmake.in:

```text
@PACKAGE_INIT@
set(MYVAR VAR)
```
1 change: 1 addition & 0 deletions docs/markdown/Module-reference.md
Expand Up @@ -2,6 +2,7 @@ Meson has a selection of modules to make common requirements easy to use.
Modules can be thought of like the standard library of a programming language.
Currently Meson provides the following modules.

* [CMake](CMake-module.md)
* [Gnome](Gnome-module.md)
* [i18n](i18n-module.md)
* [Qt4](Qt4-module.md)
Expand Down
217 changes: 217 additions & 0 deletions mesonbuild/modules/cmake.py
@@ -0,0 +1,217 @@
# Copyright 2018 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.
import re
import os, os.path
import shutil

from . import ExtensionModule, permittedSnippetKwargs, ModuleReturnValue

from .. import build, dependencies, mesonlib, mlog
from ..interpreterbase import permittedKwargs
from ..interpreter import ConfigurationDataHolder


COMPATIBILITIES = ['AnyNewerVersion', 'SameMajorVersion', 'SameMinorVersion', 'ExactVersion']

# Taken from https://github.com/Kitware/CMake/blob/master/Modules/CMakePackageConfigHelpers.cmake
PACKAGE_INIT_BASE = '''
####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
####### Any changes to this file will be overwritten by the next CMake run ####
####### The input file was @inputFileName@ ########
get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/@PACKAGE_RELATIVE_PATH@" ABSOLUTE)
'''
PACKAGE_INIT_EXT = '''
# Use original install prefix when loaded through a "/usr move"
# cross-prefix symbolic link such as /lib -> /usr/lib.
get_filename_component(_realCurr "${CMAKE_CURRENT_LIST_DIR}" REALPATH)
get_filename_component(_realOrig "@absInstallDir@" REALPATH)
if(_realCurr STREQUAL _realOrig)
set(PACKAGE_PREFIX_DIR "@installPrefix@")
endif()
unset(_realOrig)
unset(_realCurr)
'''


class CmakeModule(ExtensionModule):
cmake_detected = False
cmake_root = None

def __init__(self):
super().__init__()
self.snippets.add('configure_package_config_file')

def detect_voidp_size(self, compilers, env):
compiler = compilers.get('c', None)
if not compiler:
compiler = compilers.get('cpp', None)

if not compiler:
raise mesonlib.MesonException('Requires a C or C++ compiler to compute sizeof(void *).')

return compiler.sizeof('void *', '', env)

def detect_cmake(self):
if self.cmake_detected:
return True

cmakebin = dependencies.ExternalProgram('cmake', silent=False)
p, stdout, _stderr = mesonlib.Popen_safe(cmakebin.get_command() + ['--system-information'])[0:3]
if p.returncode != 0:
mlog.log('cmake not found, returnCode={}'.format(p.returncode))
return False

match = re.search('\n_INCLUDED_FILE \\"([^"]+)"\n', stdout.strip())
if not match:
mlog.log('unable to determine cmake root')
return False

# compilerpath is something like '/usr/share/cmake-3.5/Modules/Platform/Linux-GNU-CXX.cmake'
compilerpath = match.group(1)
pos = compilerpath.find(os.path.sep + os.path.join("Modules", "Platform", ""))
if pos < 0:
mlog.log('unknown _INCLUDED_FILE path scheme')
return False

self.cmake_root = compilerpath[0:pos]
self.cmake_detected = True
return True

@permittedKwargs({'version', 'name', 'compatibility', 'install_dir'})
def write_basic_package_version_file(self, state, _args, kwargs):
version = kwargs.get('version', None)
if not isinstance(version, str):
raise mesonlib.MesonException('Version must be specified.')

name = kwargs.get('name', None)
if not isinstance(name, str):
raise mesonlib.MesonException('Name not specified.')

compatibility = kwargs.get('compatibility', 'AnyNewerVersion')
if not isinstance(compatibility, str):
raise mesonlib.MesonException('compatibility is not string.')
if compatibility not in COMPATIBILITIES:
raise mesonlib.MesonException('compatibility must be either AnyNewerVersion, SameMajorVersion or ExactVersion.')

if not self.detect_cmake():
raise mesonlib.MesonException('Unable to find cmake')

pkgroot = kwargs.get('install_dir', None)
if pkgroot is None:
pkgroot = os.path.join(state.environment.coredata.get_builtin_option('libdir'), 'cmake', name)
if not isinstance(pkgroot, str):
raise mesonlib.MesonException('Install_dir must be a string.')

template_file = os.path.join(self.cmake_root, 'Modules', 'BasicConfigVersion-{}.cmake.in'.format(compatibility))
if not os.path.exists(template_file):
raise mesonlib.MesonException('your cmake installation doesn\'t support the {} compatibility'.format(compatibility))

version_file = os.path.join(state.environment.scratch_dir, '{}ConfigVersion.cmake'.format(name))

conf = {
'CVF_VERSION': (version, ''),
'CMAKE_SIZEOF_VOID_P': (str(self.detect_voidp_size(state.compilers, state.environment)), '')
}
mesonlib.do_conf_file(template_file, version_file, conf)

res = build.Data(mesonlib.File(True, state.environment.get_scratch_dir(), version_file), pkgroot)
return ModuleReturnValue(res, [res])

def create_package_file(self, infile, outfile, PACKAGE_RELATIVE_PATH, extra, confdata):
package_init = PACKAGE_INIT_BASE.replace('@PACKAGE_RELATIVE_PATH@', PACKAGE_RELATIVE_PATH)
package_init += extra

try:
with open(infile, "r") as fin:
data = fin.readlines()
except Exception as e:
raise mesonlib.MesonException('Could not read input file %s: %s' % (infile, str(e)))

result = []
regex = re.compile(r'(?:\\\\)+(?=\\?@)|\\@|@([-a-zA-Z0-9_]+)@')
for line in data:
line = line.replace('@PACKAGE_INIT@', package_init)
line, _missing = mesonlib.do_replacement(regex, line, confdata)

result.append(line)

outfile_tmp = outfile + "~"
with open(outfile_tmp, "w", encoding='utf-8') as fout:
fout.writelines(result)

shutil.copymode(infile, outfile_tmp)
mesonlib.replace_if_different(outfile, outfile_tmp)

@permittedSnippetKwargs({'input', 'name', 'install_dir', 'configuration'})
def configure_package_config_file(self, interpreter, state, args, kwargs):
if len(args) > 0:
raise mesonlib.MesonException('configure_package_config_file takes only keyword arguments.')

if 'input' not in kwargs:
raise mesonlib.MesonException('configure_package_config_file requires "input" keyword.')
inputfile = kwargs['input']
if isinstance(inputfile, list):
if len(inputfile) != 1:
m = "Keyword argument 'input' requires exactly one file"
raise mesonlib.MesonException(m)
inputfile = inputfile[0]
if not isinstance(inputfile, (str, mesonlib.File)):
raise mesonlib.MesonException("input must be a string or a file")
if isinstance(inputfile, str):
inputfile = mesonlib.File.from_source_file(state.environment.source_dir, state.subdir, inputfile)

ifile_abs = inputfile.absolute_path(state.environment.source_dir, state.environment.build_dir)

if 'name' not in kwargs:
raise mesonlib.MesonException('"name" not specified.')
name = kwargs['name']

(ofile_path, ofile_fname) = os.path.split(os.path.join(state.subdir, '{}Config.cmake'.format(name)))
ofile_abs = os.path.join(state.environment.build_dir, ofile_path, ofile_fname)

if 'install_dir' not in kwargs:
install_dir = os.path.join(state.environment.coredata.get_builtin_option('libdir'), 'cmake', name)
if not isinstance(install_dir, str):
raise mesonlib.MesonException('"install_dir" must be a string.')

if 'configuration' not in kwargs:
raise mesonlib.MesonException('"configuration" not specified.')
conf = kwargs['configuration']
if not isinstance(conf, ConfigurationDataHolder):
raise mesonlib.MesonException('Argument "configuration" is not of type configuration_data')

prefix = state.environment.coredata.get_builtin_option('prefix')
abs_install_dir = install_dir
if not os.path.isabs(abs_install_dir):
abs_install_dir = os.path.join(prefix, install_dir)

PACKAGE_RELATIVE_PATH = os.path.relpath(prefix, abs_install_dir)
extra = ''
if re.match('^(/usr)?/lib(64)?/.+', abs_install_dir):
extra = PACKAGE_INIT_EXT.replace('@absInstallDir@', abs_install_dir)
extra = extra.replace('@installPrefix@', prefix)

self.create_package_file(ifile_abs, ofile_abs, PACKAGE_RELATIVE_PATH, extra, conf.held_object)

conffile = os.path.normpath(inputfile.relative_name())
if conffile not in interpreter.build_def_files:
interpreter.build_def_files.append(conffile)

res = build.Data(mesonlib.File(True, ofile_path, ofile_fname), install_dir)
interpreter.build.data.append(res)

return res

def initialize():
return CmakeModule()
17 changes: 17 additions & 0 deletions test cases/common/178 cmake module/meson.build
@@ -0,0 +1,17 @@
project('cmakeModule', 'c')

cmake = import('cmake')

cmake.write_basic_package_version_file(version: '0.0.1',
name: 'cmakeModule',
)

conf = configuration_data()
conf.set('MYVAR', 'my variable value')
conf.set_quoted('MYQUOTEDVAR', 'my quoted variable value')

cmake.configure_package_config_file(
input: 'projectConfig.cmake.in',
name: 'cmakeModule',
configuration: conf,
)
4 changes: 4 additions & 0 deletions test cases/common/178 cmake module/projectConfig.cmake.in
@@ -0,0 +1,4 @@
@PACKAGE_INIT@

set(MYVAR "@MYVAR@")
set(MYQUOTEDVAR @MYQUOTEDVAR@)

0 comments on commit b31c7aa

Please sign in to comment.