2 # Copyright 2009 Jakob Westhoff. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
7 # 1. Redistributions of source code must retain the above copyright notice,
8 # this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright notice,
11 # this list of conditions and the following disclaimer in the documentation
12 # and/or other materials provided with the distribution.
14 # THIS SOFTWARE IS PROVIDED BY JAKOB WESTHOFF ``AS IS'' AND ANY EXPRESS OR
15 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 # EVENT SHALL JAKOB WESTHOFF OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 # The views and conclusions contained in the software and documentation are those
26 # of the authors and should not be interpreted as representing official policies,
27 # either expressed or implied, of Jakob Westhoff
30 include(ParseArguments)
31 find_package(Vala REQUIRED)
34 # Compile vala files to their c equivalents for further processing.
36 # The "vala_precompile" macro takes care of calling the valac executable on the
37 # given source to produce c files which can then be processed further using
38 # default cmake functions.
40 # The first parameter provided is a variable, which will be filled with a list
41 # of c files outputted by the vala compiler. This list can than be used in
42 # conjuction with functions like "add_executable" or others to create the
43 # neccessary compile rules with CMake.
45 # The initial variable is followed by a list of .vala files to be compiled.
46 # Please take care to add every vala file belonging to the currently compiled
47 # project or library as Vala will otherwise not be able to resolve all
50 # The following sections may be specified afterwards to provide certain options
51 # to the vala compiler:
54 # A list of vala packages/libraries to be used during the compile cycle. The
55 # package names are exactly the same, as they would be passed to the valac
59 # A list of optional options to be passed to the valac executable. This can be
60 # used to pass "--thread" for example to enable multi-threading support.
63 # A list of custom vapi files to be included for compilation. This can be
64 # useful to include freshly created vala libraries without having to install
68 # Pass all the needed flags to the compiler to create an internal vapi for
69 # the compiled library. The provided name will be used for this and a
70 # <provided_name>.vapi file will be created.
73 # Let the compiler generate a header file for the compiled code. There will
74 # be a header file as well as an internal header file being generated called
75 # <provided_name>.h and <provided_name>_internal.h
77 # The following call is a simple example to the vala_precompile macro showing
78 # an example to every of the optional sections:
80 # vala_precompile(VALA_C
98 # Most important is the variable VALA_C which will contain all the generated c
99 # file names after the call.
102 macro(vala_precompile output)
103 include_directories(${CMAKE_CURRENT_BINARY_DIR})
104 parse_arguments(ARGS "PACKAGES;OPTIONS;GENERATE_HEADER;GENERATE_VAPI;CUSTOM_VAPIS" "" ${ARGN})
105 set(vala_pkg_opts "")
106 foreach(pkg ${ARGS_PACKAGES})
107 list(APPEND vala_pkg_opts "--pkg=${pkg}")
108 endforeach(pkg ${ARGS_PACKAGES})
112 foreach(src ${ARGS_DEFAULT_ARGS})
113 list(APPEND in_files "${CMAKE_CURRENT_SOURCE_DIR}/${src}")
114 string(REPLACE ".vala" ".c" src ${src})
115 set(out_file "${CMAKE_CURRENT_BINARY_DIR}/${src}")
116 list(APPEND out_files "${CMAKE_CURRENT_BINARY_DIR}/${src}")
117 list(APPEND ${output} ${out_file})
118 endforeach(src ${ARGS_DEFAULT_ARGS})
120 set(vapi_arguments "")
121 if(ARGS_GENERATE_VAPI)
122 list(APPEND out_files "${CMAKE_CURRENT_BINARY_DIR}/${ARGS_GENERATE_VAPI}.vapi")
123 set(vapi_arguments "--internal-vapi=${ARGS_GENERATE_VAPI}.vapi")
125 # Header and internal header is needed to generate internal vapi
126 if (NOT ARGS_GENERATE_HEADER)
127 set(ARGS_GENERATE_HEADER ${ARGS_GENERATE_VAPI})
128 endif(NOT ARGS_GENERATE_HEADER)
129 endif(ARGS_GENERATE_VAPI)
131 set(header_arguments "")
132 if(ARGS_GENERATE_HEADER)
133 list(APPEND out_files "${CMAKE_CURRENT_BINARY_DIR}/${ARGS_GENERATE_HEADER}.h")
134 list(APPEND out_files "${CMAKE_CURRENT_BINARY_DIR}/${ARGS_GENERATE_HEADER}_internal.h")
135 list(APPEND header_arguments "--header=${ARGS_GENERATE_HEADER}.h")
136 list(APPEND header_arguments "--internal-header=${ARGS_GENERATE_HEADER}_internal.h")
137 endif(ARGS_GENERATE_HEADER)
139 # make sure output-directories exist
141 foreach(f IN LISTS out_files)
142 get_filename_component(d "${f}" PATH)
143 get_filename_component(d "${d}" ABSOLUTE)
144 list(APPEND dirs "${d}")
146 list(REMOVE_DUPLICATES dirs)
147 foreach(d IN LISTS dirs)
148 file(MAKE_DIRECTORY "${d}")
151 add_custom_command(OUTPUT ${out_files}
158 "-b" ${CMAKE_CURRENT_SOURCE_DIR}
159 "-d" ${CMAKE_CURRENT_BINARY_DIR}
168 endmacro(vala_precompile)