Merge branch 'master' of git://github.com/chunkeey/carl9170fw
[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 # Distributed under the OSI-approved BSD License (the "License");
47 # see accompanying file Copyright.txt for details.
48 #
49 # This software is distributed WITHOUT ANY WARRANTY; without even the
50 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
51 # See the License for more information.
52 #=============================================================================
53 # (To distribute this file outside of CMake, substitute the full
54 #  License text for the above reference.)
55
56 FIND_PROGRAM(GPERF_EXECUTABLE gperf DOC "path to the gperf executable")
57 MARK_AS_ADVANCED(GPERF_EXECUTABLE)
58
59 FIND_LIBRARY(FL_LIBRARY NAMES fl
60   DOC "path to the fl library")
61 MARK_AS_ADVANCED(FL_LIBRARY)
62 SET(GPERF_LIBRARIES ${FL_LIBRARY})
63
64 IF(GPERF_EXECUTABLE)
65
66   EXECUTE_PROCESS(COMMAND ${GPERF_EXECUTABLE} --version
67     OUTPUT_VARIABLE GPERF_version_output
68     ERROR_VARIABLE GPERF_version_error
69     RESULT_VARIABLE GPERF_version_result
70     OUTPUT_STRIP_TRAILING_WHITESPACE)
71
72   SET(ENV{LC_ALL} ${_Bison_SAVED_LC_ALL})
73
74   IF(NOT ${GPERF_version_result} EQUAL 0)
75     MESSAGE(SEND_ERROR "Command \"${GPERF_EXECUTABLE} --version\" failed with output:\n${GPERF_version_error}")
76   ELSE() 
77     STRING(REGEX REPLACE "^GNU gperf ([^\n]+)\n.*" "\\1"
78       GPERF_VERSION "${GPERF_version_output}")
79   ENDIF()
80
81   #============================================================
82   # GPERF_TARGET (public macro)
83   #============================================================
84   #
85   MACRO(GPERF_TARGET Name Input Output)
86     SET(GPERF_TARGET_usage "GPERF_TARGET(<Name> <Input> <Output> [COMPILE_FLAGS <string>]")
87     IF(${ARGC} GREATER 3)
88       IF(${ARGC} EQUAL 5)
89         IF("${ARGV3}" STREQUAL "COMPILE_FLAGS")
90           SET(GPERF_EXECUTABLE_opts  "${ARGV4}")
91           SEPARATE_ARGUMENTS(GPERF_EXECUTABLE_opts)
92         ELSE()
93           MESSAGE(SEND_ERROR ${GPERF_TARGET_usage})
94         ENDIF()
95       ELSE()
96         MESSAGE(SEND_ERROR ${GPERF_TARGET_usage})
97       ENDIF()
98     ENDIF()
99
100     ADD_CUSTOM_COMMAND(OUTPUT ${Output}
101       COMMAND ${GPERF_EXECUTABLE}
102       ARGS ${GPERF_EXECUTABLE_opts} < ${Input} > ${Output}
103       DEPENDS ${Input}
104       COMMENT "[GPERF][${Name}] Building hash with gperf ${GPERF_VERSION}"
105       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
106
107     SET(GPERF_${Name}_DEFINED TRUE)
108     SET(GPERF_${Name}_OUTPUTS ${Output})
109     SET(GPERF_${Name}_INPUT ${Input})
110     SET(GPERF_${Name}_COMPILE_FLAGS ${GPERF_EXECUTABLE_opts})
111   ENDMACRO(GPERF_TARGET)
112   #============================================================
113
114
115   #============================================================
116   # ADD_GPERF_BISON_DEPENDENCY (public macro)
117   #============================================================
118   #
119   MACRO(ADD_GPERF_BISON_DEPENDENCY GperfTarget BisonTarget)
120
121     IF(NOT GPERF_${GperfTarget}_OUTPUTS)
122       MESSAGE(SEND_ERROR "Gperf target `${GperfTarget}' does not exists.")
123     ENDIF()
124
125     IF(NOT BISON_${BisonTarget}_OUTPUT_HEADER)
126       MESSAGE(SEND_ERROR "Bison target `${BisonTarget}' does not exists.")
127     ENDIF()
128
129     SET_SOURCE_FILES_PROPERTIES(${GPERF_${GperfTarget}_OUTPUTS}
130       PROPERTIES OBJECT_DEPENDS ${BISON_${BisonTarget}_OUTPUT_HEADER})
131   ENDMACRO(ADD_GPERF_BISON_DEPENDENCY)
132   #============================================================
133
134 ENDIF(GPERF_EXECUTABLE)
135
136 INCLUDE(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
137 FIND_PACKAGE_HANDLE_STANDARD_ARGS(GPERF REQUIRED_VARS GPERF_EXECUTABLE
138                                        VERSION_VAR GPERF_VERSION)
139
140 # FindGPERF.cmake ends here