2
# Copyright 2009-2010 Jakob Westhoff. All rights reserved.
3
# Copyright 2012 elementary.
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions are met:
8
# 1. Redistributions of source code must retain the above copyright notice,
9
# this list of conditions and the following disclaimer.
11
# 2. Redistributions in binary form must reproduce the above copyright notice,
12
# this list of conditions and the following disclaimer in the documentation
13
# and/or other materials provided with the distribution.
15
# THIS SOFTWARE IS PROVIDED BY JAKOB WESTHOFF ``AS IS'' AND ANY EXPRESS OR
16
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18
# EVENT SHALL JAKOB WESTHOFF OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
# The views and conclusions contained in the software and documentation are those
27
# of the authors and should not be interpreted as representing official policies,
28
# either expressed or implied, of Jakob Westhoff
31
include(ParseArguments)
32
find_package(Vala REQUIRED)
35
# Compile vala files to their c equivalents for further processing.
37
# The "vala_precompile" macro takes care of calling the valac executable on the
38
# given source to produce c files which can then be processed further using
39
# default cmake functions.
41
# The first parameter provided is a variable, which will be filled with a list
42
# of c files outputted by the vala compiler. This list can than be used in
43
# conjuction with functions like "add_executable" or others to create the
44
# neccessary compile rules with CMake.
46
# The initial variable is followed by a list of .vala files to be compiled.
47
# Please take care to add every vala file belonging to the currently compiled
48
# project or library as Vala will otherwise not be able to resolve all
51
# The following sections may be specified afterwards to provide certain options
52
# to the vala compiler:
55
# A list of vala packages/libraries to be used during the compile cycle. The
56
# package names are exactly the same, as they would be passed to the valac
60
# A list of optional options to be passed to the valac executable. This can be
61
# used to pass "--thread" for example to enable multi-threading support.
64
# A list of custom vapi files to be included for compilation. This can be
65
# useful to include freshly created vala libraries without having to install
69
# Pass all the needed flags to the compiler to create an internal vapi for
70
# the compiled library. The provided name will be used for this and a
71
# <provided_name>.vapi file will be created.
74
# Let the compiler generate a header file for the compiled code. There will
75
# be a header file as well as an internal header file being generated called
76
# <provided_name>.h and <provided_name>_internal.h
79
# Have the compiler generate a GObject-Introspection repository file with
80
# name: <provided_name>.gir. This can be later used to create a binary typelib
81
# using the GI compiler.
84
# Output a <provided_name>.symbols file containing all the exported symbols.
86
# The following call is a simple example to the vala_precompile macro showing
87
# an example to every of the optional sections:
89
# vala_precompile(VALA_C mytargetname
113
# Most important is the variable VALA_C which will contain all the generated c
114
# file names after the call.
117
macro(vala_precompile output target_name)
118
parse_arguments(ARGS "TARGET;PACKAGES;OPTIONS;DIRECTORY;GENERATE_GIR;GENERATE_SYMBOLS;GENERATE_HEADER;GENERATE_VAPI;CUSTOM_VAPIS" "" ${ARGN})
121
set(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${ARGS_DIRECTORY})
123
set(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
124
endif(ARGS_DIRECTORY)
125
include_directories(${DIRECTORY})
126
set(vala_pkg_opts "")
127
foreach(pkg ${ARGS_PACKAGES})
128
list(APPEND vala_pkg_opts "--pkg=${pkg}")
129
endforeach(pkg ${ARGS_PACKAGES})
132
set(out_files_display "")
135
foreach(src ${ARGS_DEFAULT_ARGS})
136
string(REGEX MATCH "^/" IS_MATCHED ${src})
137
if(${IS_MATCHED} MATCHES "/")
138
set(src_file_path ${src})
140
set(src_file_path ${CMAKE_CURRENT_SOURCE_DIR}/${src})
142
list(APPEND in_files ${src_file_path})
143
string(REPLACE ".vala" ".c" src ${src})
144
string(REPLACE ".gs" ".c" src ${src})
145
if(${IS_MATCHED} MATCHES "/")
146
get_filename_component(VALA_FILE_NAME ${src} NAME)
147
set(out_file "${CMAKE_CURRENT_BINARY_DIR}/${VALA_FILE_NAME}")
148
list(APPEND out_files "${CMAKE_CURRENT_BINARY_DIR}/${VALA_FILE_NAME}")
150
set(out_file "${DIRECTORY}/${src}")
151
list(APPEND out_files "${DIRECTORY}/${src}")
153
list(APPEND ${output} ${out_file})
154
list(APPEND out_files_display "${src}")
155
endforeach(src ${ARGS_DEFAULT_ARGS})
157
set(custom_vapi_arguments "")
158
if(ARGS_CUSTOM_VAPIS)
159
foreach(vapi ${ARGS_CUSTOM_VAPIS})
160
if(${vapi} MATCHES ${CMAKE_SOURCE_DIR} OR ${vapi} MATCHES ${CMAKE_BINARY_DIR})
161
list(APPEND custom_vapi_arguments ${vapi})
162
else (${vapi} MATCHES ${CMAKE_SOURCE_DIR} OR ${vapi} MATCHES ${CMAKE_BINARY_DIR})
163
list(APPEND custom_vapi_arguments ${CMAKE_CURRENT_SOURCE_DIR}/${vapi})
164
endif(${vapi} MATCHES ${CMAKE_SOURCE_DIR} OR ${vapi} MATCHES ${CMAKE_BINARY_DIR})
165
endforeach(vapi ${ARGS_CUSTOM_VAPIS})
166
endif(ARGS_CUSTOM_VAPIS)
168
set(vapi_arguments "")
169
if(ARGS_GENERATE_VAPI)
170
list(APPEND out_files "${DIRECTORY}/${ARGS_GENERATE_VAPI}.vapi")
171
list(APPEND out_files_display "${ARGS_GENERATE_VAPI}.vapi")
172
set(vapi_arguments "--library=${ARGS_GENERATE_VAPI}" "--vapi=${ARGS_GENERATE_VAPI}.vapi")
174
# Header and internal header is needed to generate internal vapi
175
if (NOT ARGS_GENERATE_HEADER)
176
set(ARGS_GENERATE_HEADER ${ARGS_GENERATE_VAPI})
177
endif(NOT ARGS_GENERATE_HEADER)
178
endif(ARGS_GENERATE_VAPI)
180
set(header_arguments "")
181
if(ARGS_GENERATE_HEADER)
182
list(APPEND out_files "${DIRECTORY}/${ARGS_GENERATE_HEADER}.h")
183
list(APPEND out_files_display "${ARGS_GENERATE_HEADER}.h")
184
list(APPEND header_arguments "--header=${ARGS_GENERATE_HEADER}.h")
185
endif(ARGS_GENERATE_HEADER)
187
set(gir_arguments "")
188
set(gircomp_command "")
189
if(ARGS_GENERATE_GIR)
190
list(APPEND out_files "${DIRECTORY}/${ARGS_GENERATE_GIR}.gir")
191
list(APPEND out_files_display "${ARGS_GENERATE_GIR}.gir")
192
set(gir_arguments "--gir=${ARGS_GENERATE_GIR}.gir")
194
include (FindGirCompiler)
195
find_package(GirCompiler REQUIRED)
199
${G_IR_COMPILER_EXECUTABLE}
201
"${DIRECTORY}/${ARGS_GENERATE_GIR}.gir"
202
-o "${DIRECTORY}/${ARGS_GENERATE_GIR}.typelib")
203
endif(ARGS_GENERATE_GIR)
205
set(symbols_arguments "")
206
if(ARGS_GENERATE_SYMBOLS)
207
list(APPEND out_files "${DIRECTORY}/${ARGS_GENERATE_SYMBOLS}.symbols")
208
list(APPEND out_files_display "${ARGS_GENERATE_SYMBOLS}.symbols")
209
set(symbols_arguments "--symbols=${ARGS_GENERATE_SYMBOLS}.symbols")
210
endif(ARGS_GENERATE_SYMBOLS)
212
# Workaround for a bug that would make valac run twice. This file is written
213
# after the vala compiler generates C source code.
214
set(OUTPUT_STAMP ${CMAKE_CURRENT_BINARY_DIR}/${target_name}_valac.stamp)
227
"-b" ${CMAKE_CURRENT_SOURCE_DIR}
234
${custom_vapi_arguments}
243
"Generating ${out_files_display}"
247
# This command will be run twice for some reason (pass a non-empty string to COMMENT
248
# in order to see it). Since valac is not executed from here, this won't be a problem.
249
add_custom_command(OUTPUT ${out_files} DEPENDS ${OUTPUT_STAMP} COMMENT "")
250
endmacro(vala_precompile)