041a527be385fe6a7730da73f4d6ea034fe96746
[carl9170fw.git] / extra / FindGPERF.cmake
1 # - Find gperf executable and provides a macro to generate custom build rules
2 #
3 # The module defines the following variables:
4 #  GPERF_FOUND - true is gperf executable is found
5 #  GPERF_EXECUTABLE - the path to the gperf executable
6 #  GPERF_VERSION - the version of gperf
7 #  GPERF_LIBRARIES - The gperf libraries
8 #
9 # The minimum required version of gperf can be specified using the
10 # standard syntax, e.g. FIND_PACKAGE(GPERF 2.5.13)
11 #
12 #
13 # If gperf is found on the system, the module provides the macro:
14 #  GPERF_TARGET(Name GperfInput GperfOutput [COMPILE_FLAGS <string>])
15 # which creates a custom command  to generate the <GperfOutput> file from
16 # the <GperfInput> file.  If  COMPILE_FLAGS option is specified, the next
17 # parameter is added to the gperf  command line. Name is an alias used to
18 # get  details of  this custom  command.  Indeed the  macro defines  the
19 # following variables:
20 #  GPERF_${Name}_DEFINED - true is the macro ran successfully
21 #  GPERF_${Name}_OUTPUTS - the source file generated by the custom rule, an
22 #  alias for GperfOutput
23 #  GPERF_${Name}_INPUT - the gperf source file, an alias for ${GperfInput}
24 #
25 # Gperf scanners oftenly use tokens  defined by Bison: the code generated
26 # by Gperf  depends of the header  generated by Bison.   This module also
27 # defines a macro:
28 #  ADD_GPERF_BISON_DEPENDENCY(GperfTarget BisonTarget)
29 # which  adds the  required dependency  between a  scanner and  a parser
30 # where  <GperfTarget>  and <BisonTarget>  are  the  first parameters  of
31 # respectively GPERF_TARGET and BISON_TARGET macros.
32 #
33 #  ====================================================================
34 #  Example:
35 #
36 #   find_package(GPERF)
37 #
38 #   GPERF_TARGET(MyHash hash.gperf  ${CMAKE_CURRENT_BINARY_DIR}/hash.c)
39 #
40 #  ====================================================================
41
42 #=============================================================================
43 # Copyright 2009 Kitware, Inc.
44 # Copyright 2006 Tristan Carel
45 #
46 # Redistribution and use in source and binary forms, with or without
47 # modification, are permitted provided that the following conditions
48 # are met:
49 #
50 # * Redistributions of source code must retain the above copyright
51 #   notice, this list of conditions and the following disclaimer.
52 #
53 # * Redistributions in binary form must reproduce the above copyright
54 #   notice, this list of conditions and the following disclaimer in the
55 #   documentation and/or other materials provided with the distribution.
56 #
57 # * Neither the names of Kitware, Inc., the Insight Software Consortium,
58 #   nor the names of their contributors may be used to endorse or promote
59 #   products derived from this software without specific prior written
60 #   permission.
61 #
62 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
63 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
64 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
65 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
66 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
67 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
68 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
69 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
70 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
71 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
72 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73
74 FIND_PROGRAM(GPERF_EXECUTABLE gperf DOC "path to the gperf executable")
75 MARK_AS_ADVANCED(GPERF_EXECUTABLE)
76
77 FIND_LIBRARY(FL_LIBRARY NAMES fl
78   DOC "path to the fl library")
79 MARK_AS_ADVANCED(FL_LIBRARY)
80 SET(GPERF_LIBRARIES ${FL_LIBRARY})
81
82 IF(GPERF_EXECUTABLE)
83
84   EXECUTE_PROCESS(COMMAND ${GPERF_EXECUTABLE} --version
85     OUTPUT_VARIABLE GPERF_version_output
86     ERROR_VARIABLE GPERF_version_error
87     RESULT_VARIABLE GPERF_version_result
88     OUTPUT_STRIP_TRAILING_WHITESPACE)
89
90   SET(ENV{LC_ALL} ${_Bison_SAVED_LC_ALL})
91
92   IF(NOT ${GPERF_version_result} EQUAL 0)
93     MESSAGE(SEND_ERROR "Command \"${GPERF_EXECUTABLE} --version\" failed with output:\n${GPERF_version_error}")
94   ELSE() 
95     STRING(REGEX REPLACE "^GNU gperf ([^\n]+)\n.*" "\\1"
96       GPERF_VERSION "${GPERF_version_output}")
97   ENDIF()
98
99   #============================================================
100   # GPERF_TARGET (public macro)
101   #============================================================
102   #
103   MACRO(GPERF_TARGET Name Input Output)
104     SET(GPERF_TARGET_usage "GPERF_TARGET(<Name> <Input> <Output> [COMPILE_FLAGS <string>]")
105     IF(${ARGC} GREATER 3)
106       IF(${ARGC} EQUAL 5)
107         IF("${ARGV3}" STREQUAL "COMPILE_FLAGS")
108           SET(GPERF_EXECUTABLE_opts  "${ARGV4}")
109           SEPARATE_ARGUMENTS(GPERF_EXECUTABLE_opts)
110         ELSE()
111           MESSAGE(SEND_ERROR ${GPERF_TARGET_usage})
112         ENDIF()
113       ELSE()
114         MESSAGE(SEND_ERROR ${GPERF_TARGET_usage})
115       ENDIF()
116     ENDIF()
117
118     ADD_CUSTOM_COMMAND(OUTPUT ${Output}
119       COMMAND ${GPERF_EXECUTABLE}
120       ARGS ${GPERF_EXECUTABLE_opts} < ${Input} > ${Output}
121       DEPENDS ${Input}
122       COMMENT "[GPERF][${Name}] Building hash with gperf ${GPERF_VERSION}"
123       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
124
125     SET(GPERF_${Name}_DEFINED TRUE)
126     SET(GPERF_${Name}_OUTPUTS ${Output})
127     SET(GPERF_${Name}_INPUT ${Input})
128     SET(GPERF_${Name}_COMPILE_FLAGS ${GPERF_EXECUTABLE_opts})
129   ENDMACRO(GPERF_TARGET)
130   #============================================================
131
132
133   #============================================================
134   # ADD_GPERF_BISON_DEPENDENCY (public macro)
135   #============================================================
136   #
137   MACRO(ADD_GPERF_BISON_DEPENDENCY GperfTarget BisonTarget)
138
139     IF(NOT GPERF_${GperfTarget}_OUTPUTS)
140       MESSAGE(SEND_ERROR "Gperf target `${GperfTarget}' does not exists.")
141     ENDIF()
142
143     IF(NOT BISON_${BisonTarget}_OUTPUT_HEADER)
144       MESSAGE(SEND_ERROR "Bison target `${BisonTarget}' does not exists.")
145     ENDIF()
146
147     SET_SOURCE_FILES_PROPERTIES(${GPERF_${GperfTarget}_OUTPUTS}
148       PROPERTIES OBJECT_DEPENDS ${BISON_${BisonTarget}_OUTPUT_HEADER})
149   ENDMACRO(ADD_GPERF_BISON_DEPENDENCY)
150   #============================================================
151
152 ENDIF(GPERF_EXECUTABLE)
153
154 INCLUDE(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
155 FIND_PACKAGE_HANDLE_STANDARD_ARGS(GPERF REQUIRED_VARS GPERF_EXECUTABLE
156                                        VERSION_VAR GPERF_VERSION)
157
158 # FindGPERF.cmake ends here