Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CMake find script for using ctemplate from CMake projects. #120

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions contrib/FindCtemplate.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# - Try to find the ctemplate
# Once done this will define
#
# CTEMPLATE_FOUND - system has ctemplate
# CTEMPLATE_INCLUDE_DIR - the ctemplate include directory
# CTEMPLATE_LIBRARIES - Link this to use ctemplate
# CTEMPLATE_COMPILER - CTemplate compiler executable
#
# CTEMPLATE_VARNAMES - Macro that wraps templates in varnames.h
# Usage: CTEMPLATE_VARNAMES(header1.h header2.h ${CT_FILES})
#

# Copyright (c) 2009, Thomas Richard, <[email protected]>
# Copyright (c) 2012, Siemens <[email protected]>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.

if (CTEMPLATE_INCLUDE_DIR AND CTEMPLATE_LIBRARIES)
# in cache already
set(CTEMPLATE_FOUND TRUE)

else (CTEMPLATE_INCLUDE_DIR AND CTEMPLATE_LIBRARIES)
find_path(CTEMPLATE_INCLUDE_DIR ctemplate/template.h)
find_library(CTEMPLATE_LIBRARIES NAMES ctemplate)
find_program(CTEMPLATE_COMPILER make_tpl_varnames_h)

include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Ctemplate DEFAULT_MSG CTEMPLATE_INCLUDE_DIR CTEMPLATE_LIBRARIES )

endif (CTEMPLATE_INCLUDE_DIR AND CTEMPLATE_LIBRARIES)


macro (CTEMPLATE_VARNAMES outfiles)

foreach (infile ${ARGN})
get_filename_component(infile ${infile} ABSOLUTE)
CTEMPLATE_MAKE_OUTPUT_file(${infile} varnames.h outfile)
add_custom_command(OUTPUT ${outfile}
COMMAND ${CTEMPLATE_COMPILER} -q -f ${outfile} ${infile}
DEPENDS ${infile} VERBATIM)
set(${outfiles} ${${outfiles}} ${outfile})
endforeach(infile)

endmacro (CTEMPLATE_VARNAMES outfiles)

# macro used to create the names of output files preserving relative dirs
macro (CTEMPLATE_MAKE_OUTPUT_file infile ext outfile )
string(LENGTH ${CMAKE_CURRENT_BINARY_DIR} _binlength)
string(LENGTH ${infile} _infileLength)
set(_checkinfile ${CMAKE_CURRENT_SOURCE_DIR})
if(_infileLength GREATER _binlength)
string(SUBSTRING "${infile}" 0 ${_binlength} _checkinfile)
if(_checkinfile STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
file(RELATIVE_PATH rel ${CMAKE_CURRENT_BINARY_DIR} ${infile})
else(_checkinfile STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
endif(_checkinfile STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
else(_infileLength GREATER _binlength)
file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
endif(_infileLength GREATER _binlength)
if(WIN32 AND rel MATCHES "^[a-zA-Z]:") # absolute path
string(REGEX REPLACE "^([a-zA-Z]):(.*)$" "\\1_\\2" rel "${rel}")
endif(WIN32 AND rel MATCHES "^[a-zA-Z]:")
set(_outfile "${CMAKE_CURRENT_BINARY_DIR}/${rel}")
string(REPLACE ".." "__" _outfile ${_outfile})
get_filename_component(outpath ${_outfile} PATH)
get_filename_component(_outfile ${_outfile} NAME)
file(MAKE_DIRECTORY ${outpath})
set(${outfile} ${outpath}/${_outfile}.${ext})
endmacro (CTEMPLATE_MAKE_OUTPUT_file)