~elementary-apps/pantheon-terminal/master

« back to all changes in this revision

Viewing changes to cmake/vala/ValaPrecompile.cmake

  • Committer: kekun.plazas@laposte.net
  • Date: 2011-06-19 15:34:04 UTC
  • Revision ID: git-v1:03007337b8d45983a3be21b8d295b5af68a60b04
CMake port in progress

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##
 
2
# Copyright 2009-2010 Jakob Westhoff. All rights reserved.
 
3
 
4
# Redistribution and use in source and binary forms, with or without
 
5
# modification, are permitted provided that the following conditions are met:
 
6
 
7
#    1. Redistributions of source code must retain the above copyright notice,
 
8
#       this list of conditions and the following disclaimer.
 
9
 
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.
 
13
 
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.
 
24
 
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
 
28
##
 
29
 
 
30
include(ParseArguments)
 
31
find_package(Vala REQUIRED)
 
32
 
 
33
##
 
34
# Compile vala files to their c equivalents for further processing. 
 
35
#
 
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.
 
39
 
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.
 
44
 
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
 
48
# dependencies.
 
49
 
50
# The following sections may be specified afterwards to provide certain options
 
51
# to the vala compiler:
 
52
 
53
# PACKAGES
 
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
 
56
#   "--pkg=" option.
 
57
 
58
# OPTIONS
 
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.
 
61
#
 
62
# CUSTOM_VAPIS
 
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
 
65
#   them in the system.
 
66
#
 
67
# GENERATE_VAPI
 
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.
 
71
 
72
# GENERATE_HEADER
 
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
 
76
#
 
77
# The following call is a simple example to the vala_precompile macro showing
 
78
# an example to every of the optional sections:
 
79
#
 
80
#   vala_precompile(VALA_C
 
81
#       source1.vala
 
82
#       source2.vala
 
83
#       source3.vala
 
84
#   PACKAGES
 
85
#       gtk+-2.0
 
86
#       gio-1.0
 
87
#       posix
 
88
#   DIRECTORY
 
89
#       gen
 
90
#   OPTIONS
 
91
#       --thread
 
92
#   CUSTOM_VAPIS
 
93
#       some_vapi.vapi
 
94
#   GENERATE_VAPI
 
95
#       myvapi
 
96
#   GENERATE_HEADER
 
97
#       myheader
 
98
#   )
 
99
#
 
100
# Most important is the variable VALA_C which will contain all the generated c
 
101
# file names after the call.
 
102
##
 
103
 
 
104
macro(vala_precompile output)
 
105
    parse_arguments(ARGS "PACKAGES;OPTIONS;DIRECTORY;GENERATE_HEADER;GENERATE_VAPI;CUSTOM_VAPIS" "" ${ARGN})
 
106
    if(ARGS_DIRECTORY)
 
107
        set(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${ARGS_DIRECTORY})
 
108
    else(ARGS_DIRECTORY)
 
109
        set(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
 
110
    endif(ARGS_DIRECTORY)
 
111
    include_directories(${DIRECTORY})
 
112
    set(vala_pkg_opts "")
 
113
    foreach(pkg ${ARGS_PACKAGES})
 
114
        list(APPEND vala_pkg_opts "--pkg=${pkg}")
 
115
    endforeach(pkg ${ARGS_PACKAGES})
 
116
    set(in_files "")
 
117
    set(out_files "")
 
118
    set(${output} "")
 
119
    foreach(src ${ARGS_DEFAULT_ARGS})
 
120
        list(APPEND in_files "${CMAKE_CURRENT_SOURCE_DIR}/${src}")
 
121
        string(REPLACE ".vala" ".c" src ${src})
 
122
        string(REPLACE ".gs" ".c" src ${src})
 
123
        set(out_file "${DIRECTORY}/${src}")
 
124
        list(APPEND out_files "${DIRECTORY}/${src}")
 
125
        list(APPEND ${output} ${out_file})
 
126
    endforeach(src ${ARGS_DEFAULT_ARGS})
 
127
 
 
128
    set(custom_vapi_arguments "")
 
129
    if(ARGS_CUSTOM_VAPIS)
 
130
        foreach(vapi ${ARGS_CUSTOM_VAPIS})
 
131
            if(${vapi} MATCHES ${CMAKE_SOURCE_DIR} OR ${vapi} MATCHES ${CMAKE_BINARY_DIR})
 
132
                list(APPEND custom_vapi_arguments ${vapi})
 
133
            else (${vapi} MATCHES ${CMAKE_SOURCE_DIR} OR ${vapi} MATCHES ${CMAKE_BINARY_DIR})
 
134
                list(APPEND custom_vapi_arguments ${CMAKE_CURRENT_SOURCE_DIR}/${vapi})
 
135
            endif(${vapi} MATCHES ${CMAKE_SOURCE_DIR} OR ${vapi} MATCHES ${CMAKE_BINARY_DIR})
 
136
        endforeach(vapi ${ARGS_CUSTOM_VAPIS})
 
137
    endif(ARGS_CUSTOM_VAPIS)
 
138
 
 
139
    set(vapi_arguments "")
 
140
    if(ARGS_GENERATE_VAPI)
 
141
        list(APPEND out_files "${DIRECTORY}/${ARGS_GENERATE_VAPI}.vapi")
 
142
        set(vapi_arguments "--internal-vapi=${ARGS_GENERATE_VAPI}.vapi")
 
143
 
 
144
        # Header and internal header is needed to generate internal vapi
 
145
        if (NOT ARGS_GENERATE_HEADER)
 
146
            set(ARGS_GENERATE_HEADER ${ARGS_GENERATE_VAPI})
 
147
        endif(NOT ARGS_GENERATE_HEADER)
 
148
    endif(ARGS_GENERATE_VAPI)
 
149
 
 
150
    set(header_arguments "")
 
151
    if(ARGS_GENERATE_HEADER)
 
152
        list(APPEND out_files "${DIRECTORY}/${ARGS_GENERATE_HEADER}.h")
 
153
        list(APPEND out_files "${DIRECTORY}/${ARGS_GENERATE_HEADER}_internal.h")
 
154
        list(APPEND header_arguments "--header=${DIRECTORY}/${ARGS_GENERATE_HEADER}.h")
 
155
        list(APPEND header_arguments "--internal-header=${DIRECTORY}/${ARGS_GENERATE_HEADER}_internal.h")
 
156
    endif(ARGS_GENERATE_HEADER)
 
157
 
 
158
    add_custom_command(OUTPUT ${out_files} 
 
159
    COMMAND 
 
160
        ${VALA_EXECUTABLE} 
 
161
    ARGS 
 
162
        "-C" 
 
163
        ${header_arguments} 
 
164
        ${vapi_arguments}
 
165
        "-b" ${CMAKE_CURRENT_SOURCE_DIR} 
 
166
        "-d" ${DIRECTORY} 
 
167
        ${vala_pkg_opts} 
 
168
        ${ARGS_OPTIONS} 
 
169
        ${in_files} 
 
170
        ${custom_vapi_arguments}
 
171
    DEPENDS 
 
172
        ${in_files} 
 
173
        ${ARGS_CUSTOM_VAPIS}
 
174
    )
 
175
endmacro(vala_precompile)