1 # - Find gperf executable and provides a macro to generate custom build rules
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
9 # The minimum required version of gperf can be specified using the
10 # standard syntax, e.g. FIND_PACKAGE(GPERF 2.5.13)
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}
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
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.
33 # ====================================================================
38 # GPERF_TARGET(MyHash hash.gperf ${CMAKE_CURRENT_BINARY_DIR}/hash.c)
40 # ====================================================================
42 #=============================================================================
43 # Copyright 2009 Kitware, Inc.
44 # Copyright 2006 Tristan Carel
46 # Redistribution and use in source and binary forms, with or without
47 # modification, are permitted provided that the following conditions
50 # * Redistributions of source code must retain the above copyright
51 # notice, this list of conditions and the following disclaimer.
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.
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
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.
74 cmake_minimum_required(VERSION 2.8.4)
76 FIND_PROGRAM(GPERF_EXECUTABLE gperf DOC "path to the gperf executable")
77 MARK_AS_ADVANCED(GPERF_EXECUTABLE)
79 FIND_LIBRARY(FL_LIBRARY NAMES fl
80 DOC "path to the fl library")
81 MARK_AS_ADVANCED(FL_LIBRARY)
82 SET(GPERF_LIBRARIES ${FL_LIBRARY})
86 EXECUTE_PROCESS(COMMAND ${GPERF_EXECUTABLE} --version
87 OUTPUT_VARIABLE GPERF_version_output
88 ERROR_VARIABLE GPERF_version_error
89 RESULT_VARIABLE GPERF_version_result
90 OUTPUT_STRIP_TRAILING_WHITESPACE)
92 SET(ENV{LC_ALL} ${_Bison_SAVED_LC_ALL})
94 IF(NOT ${GPERF_version_result} EQUAL 0)
95 MESSAGE(SEND_ERROR "Command \"${GPERF_EXECUTABLE} --version\" failed with output:\n${GPERF_version_error}")
97 STRING(REGEX REPLACE "^GNU gperf ([^\n]+)\n.*" "\\1"
98 GPERF_VERSION "${GPERF_version_output}")
101 #============================================================
102 # GPERF_TARGET (public macro)
103 #============================================================
105 MACRO(GPERF_TARGET Name Input Output)
106 SET(GPERF_TARGET_usage "GPERF_TARGET(<Name> <Input> <Output> [COMPILE_FLAGS <string>]")
107 IF(${ARGC} GREATER 3)
109 IF("${ARGV3}" STREQUAL "COMPILE_FLAGS")
110 SET(GPERF_EXECUTABLE_opts "${ARGV4}")
111 SEPARATE_ARGUMENTS(GPERF_EXECUTABLE_opts)
113 MESSAGE(SEND_ERROR ${GPERF_TARGET_usage})
116 MESSAGE(SEND_ERROR ${GPERF_TARGET_usage})
120 ADD_CUSTOM_COMMAND(OUTPUT ${Output}
121 COMMAND ${GPERF_EXECUTABLE}
122 ARGS ${GPERF_EXECUTABLE_opts} < ${Input} > ${Output}
124 COMMENT "[GPERF][${Name}] Building hash with gperf ${GPERF_VERSION}"
125 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
127 SET(GPERF_${Name}_DEFINED TRUE)
128 SET(GPERF_${Name}_OUTPUTS ${Output})
129 SET(GPERF_${Name}_INPUT ${Input})
130 SET(GPERF_${Name}_COMPILE_FLAGS ${GPERF_EXECUTABLE_opts})
131 ENDMACRO(GPERF_TARGET)
132 #============================================================
135 #============================================================
136 # ADD_GPERF_BISON_DEPENDENCY (public macro)
137 #============================================================
139 MACRO(ADD_GPERF_BISON_DEPENDENCY GperfTarget BisonTarget)
141 IF(NOT GPERF_${GperfTarget}_OUTPUTS)
142 MESSAGE(SEND_ERROR "Gperf target `${GperfTarget}' does not exists.")
145 IF(NOT BISON_${BisonTarget}_OUTPUT_HEADER)
146 MESSAGE(SEND_ERROR "Bison target `${BisonTarget}' does not exists.")
149 SET_SOURCE_FILES_PROPERTIES(${GPERF_${GperfTarget}_OUTPUTS}
150 PROPERTIES OBJECT_DEPENDS ${BISON_${BisonTarget}_OUTPUT_HEADER})
151 ENDMACRO(ADD_GPERF_BISON_DEPENDENCY)
152 #============================================================
154 ENDIF(GPERF_EXECUTABLE)
156 INCLUDE(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
157 FIND_PACKAGE_HANDLE_STANDARD_ARGS(GPERF REQUIRED_VARS GPERF_EXECUTABLE
158 VERSION_VAR GPERF_VERSION)
160 # FindGPERF.cmake ends here