-
Notifications
You must be signed in to change notification settings - Fork 13
/
CMakeLists.txt
216 lines (183 loc) · 7.24 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# (C) Copyright 2018- ECMWF.
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
##############################################################################
#.rst:
#
# loki
# ====
#
# Install Loki with dependencies. ::
#
# Features
# --------
#
# :NO_INSTALL: Do not install Loki itself but make the CMake configuration
# available (Default: ``OFF``)
# :EDITABLE: Install Loki as an editable package (Default: ``OFF``)
# :CLAW: Install CLAW and OMNI Compiler (Default: ``OFF``)
#
# Installation procedure
# ----------------------
#
# A virtual environment is created for Loki into which it is installed along
# with any dependencies. The CLI scripts ``loki-transform.py`` and ``loki-lint.py``
# are made available as executable targets, thus can be used from any subsequent
# ``add_custom_command`` statements.
#
##############################################################################
# Version 3.12 required to use FindPython
# Version 3.15 officially required to use Python3_FIND_VIRTUALENV (not working on 3.15.3,
# though, and use 3.17 for conda support anyway)
# Version 3.19 for support of find_package version range and file(CHMOD)
cmake_minimum_required( VERSION 3.19 FATAL_ERROR )
find_package( ecbuild 3.7 REQUIRED )
# Specify project and configuration options
project( loki LANGUAGES NONE )
# Allow negating ENABLE_NO_INSTALL with a leading '~'
macro( apply_negation VAR_NAME )
if( DEFINED ${VAR_NAME} )
if( ${${VAR_NAME}} MATCHES ^~ )
string( REPLACE ~ "" ${VAR_NAME} ${${VAR_NAME}} )
if( ${${VAR_NAME}} )
set( ${VAR_NAME} OFF )
else()
set( ${VAR_NAME} ON )
endif()
endif()
endif()
endmacro()
apply_negation( ENABLE_NO_INSTALL )
apply_negation( LOKI_ENABLE_NO_INSTALL )
# Declare options
ecbuild_add_option(
FEATURE NO_INSTALL
DEFAULT OFF
DESCRIPTION "Disable Loki (and dependency) installation"
)
ecbuild_add_option(
FEATURE EDITABLE
DEFAULT OFF
DESCRIPTION "Install Loki as an editable Python package"
)
ecbuild_add_option(
FEATURE CLAW
DEFAULT OFF
DESCRIPTION "Build OMNI compiler and CLAW compiler"
)
ecbuild_add_option(
FEATURE OMNI
DEFAULT OFF
DESCRIPTION "Build OMNI compiler as Loki frontend"
CONDITION NOT HAVE_CLAW
)
include( loki_transform )
# Make CMake script files available in build and install directory
add_subdirectory( cmake )
install( DIRECTORY cmake DESTINATION ${INSTALL_DATA_DIR} PATTERN "CMakeLists.txt" EXCLUDE )
# The list of Loki frontend scripts
file( GLOB _LOKI_SCRIPTS "${CMAKE_CURRENT_SOURCE_DIR}/scripts/loki_*.py" )
list( TRANSFORM _LOKI_SCRIPTS REPLACE "scripts/loki_" "scripts/loki-" )
set( LOKI_EXECUTABLES "" )
foreach( _exe IN LISTS _LOKI_SCRIPTS )
get_filename_component( _exe_name ${_exe} NAME )
list( APPEND LOKI_EXECUTABLES ${_exe_name} )
endforeach()
# Install Loki and dependencies
if( NOT HAVE_NO_INSTALL )
if( HAVE_CLAW )
include( claw_compiler )
install_claw_compiler( mlange-dev )
endif()
if( HAVE_OMNI )
include( omni_compiler )
install_omni_compiler( master )
endif()
# Setup Python virtual environment
include( python_venv )
set( loki_VENV_PATH ${CMAKE_CURRENT_BINARY_DIR}/loki_env )
set( PYTHON_VERSION 3.8...<3.12 )
setup_python_venv( ${loki_VENV_PATH} ${PYTHON_VERSION} )
install( DIRECTORY ${loki_VENV_PATH} DESTINATION . USE_SOURCE_PERMISSIONS )
# Enable Pytest tests as ecbuild/ctest targets
if( HAVE_TESTS )
if( HAVE_CLAW )
set( _TEST_SELECTOR "not ofp" )
set( _TEST_PATH "${CLAW_DIR}/bin:$ENV{PATH}" )
elseif( HAVE_OMNI )
set( _TEST_SELECTOR "not ofp" )
set( _TEST_PATH "${OMNI_DIR}/bin:$ENV{PATH}" )
else()
set( _TEST_SELECTOR "not ofp and not omni" )
set( _TEST_PATH "$ENV{PATH}" )
endif()
# Nesting the CMake tests into CTest does not correctly resolve
# search paths, therefore these are getting disabled here
set( _TEST_SELECTOR "${_TEST_SELECTOR} and not cmake")
# ecbuild_add_test relies on the variables set by the _very_ outdated
# FindPythonInterp, so we set the bare minimum here using the values
# from our FindPython3 variables
set( PYTHONINTERP_FOUND True )
set( PYTHON_EXECUTABLE ${Python3_EXECUTABLE} )
ecbuild_add_test(
TYPE PYTHON
TARGET loki_tests
ARGS -m pytest -k ${_TEST_SELECTOR} -v
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
ENVIRONMENT PATH=${Python3_VENV_BIN}:${_TEST_PATH}
)
ecbuild_add_test(
TYPE PYTHON
TARGET loki_lint_rules
ARGS -m pytest -k ${_TEST_SELECTOR} -v
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lint_rules
ENVIRONMENT PATH=${Python3_VENV_BIN}:${_TEST_PATH}
)
list( APPEND LOKI_INSTALL_OPTIONS "tests" )
endif()
# Determine whether this is a Git worktree or if we have to provide
# the version number to setuptools_scm
if( NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git )
set( ENV{SETUPTOOLS_SCM_PRETEND_VERSION} ${loki_VERSION} )
endif()
# Install Loki python package with dependencies
if( NOT ${CMAKE_VERBOSE_MAKEFILE} )
# Less verbose output
list ( APPEND PIP_OPTIONS "-q" )
endif()
execute_process( COMMAND ${Python3_EXECUTABLE} -m pip install --upgrade ${PIP_OPTIONS} pip )
if( HAVE_EDITABLE )
# Use checked-out source instead of installing into venv
list( APPEND PIP_OPTIONS "-e" )
endif()
set( _INSTALL_OPTIONS "" )
if( LOKI_INSTALL_OPTIONS )
list( JOIN LOKI_INSTALL_OPTIONS "," _INSTALL_OPT_STR )
set( _INSTALL_OPTIONS "[${_INSTALL_OPT_STR}]" )
endif()
# We install Loki at configure time (for now), since bulk-transformation planning
# requires configure time execution to allow injection with CMake targets.
ecbuild_info( "Install Loki in virtual environment" )
execute_process( COMMAND ${Python3_EXECUTABLE} -m pip install ${PIP_OPTIONS} ${CMAKE_CURRENT_SOURCE_DIR}${_INSTALL_OPTIONS} )
ecbuild_info( "Install Loki in virtual environment - done" )
ecbuild_info( "Install Loki lint_rules in virtual environment" )
execute_process( COMMAND ${Python3_EXECUTABLE} -m pip install ${PIP_OPTIONS} ${CMAKE_CURRENT_SOURCE_DIR}/lint_rules )
ecbuild_info( "Install Loki lint_rules in virtual environment - done" )
# Make the shebang in Python scripts use relative paths
list(
TRANSFORM LOKI_EXECUTABLES
PREPEND ${Python3_VENV_BIN}/
OUTPUT_VARIABLE _venv_executables
)
update_python_shebang( ${_venv_executables} )
endif()
# Discover Loki executables and make available as CMake targets
include( loki_find_executables )
loki_find_executables()
# Install the project so it can be used within the bundle
ecbuild_install_project( NAME loki )
# print summary
ecbuild_print_summary()