[xsd-users] cmake module

Benjamin Schindler bschindler at inf.ethz.ch
Tue May 11 07:12:24 EDT 2010


Hi

I'm using xsd as one of my projects and I wrote a small module for cmake
that will compile your files for you. The usage is similar to the qt
module. Sobasically what you do is:

WRAP_XSD(SERVER_XSDS_SRCS SERVER_XSDS_INCLUDE
${XSD_OUTPUT_DIR}/schema/core/server ${SERVER_XSDS} OPTIONS
${SERVER_XSD_ARGS})
INCLUDE_DIRECTORIES(${SERVER_XSDS_INCLUDE})
ADD_LIBRARY(server SHARED ${SERVER_SRCS} ${SERVER_XSDS_SRCS})

What it does is it copies the xsd over to the build directory and
compile it from there. This might seem unintuitive but I wanted to have
a self-contained build dir so I can support validation. Also I wanted to
more easily support install targets (which I do not really yet).
This means that if you include a schema which includes some other schema
you wrote, the include path need to match the way the files are laid out
in the build directory and not the source directory.

I'm posting this here mostly to get some feedback and may be it's of use
to some other people. It has taken a non-unsignificant amount of time to
write :)

It has been test in a linux-environment and under visual studio

Cheers
Benjamin
-------------- next part --------------
# - Find CodeSource Xsd
# This module can be used to find Xsd and it's include path
# Variables:
#	XSD_EXECUTABLE
#	XSD_INCLUDE_DIR
#	XSD_FOUND

SET(XSD_FOUND FALSE)

if(WIN32)
	SET(__XSD_NAME xsd.exe)
else(WIN32)
	SET(__XSD_NAME xsd)
endif(WIN32)

if(XSD_INCLUDE_DIR)
	#in cache already
	SET(XSD_FOUND TRUE)
else(XSD_INCLUDE_DIR)
	find_file(XSD_EXECUTABLE NAMES ${__XSD_NAME}
	     PATHS
		 ${XSD_DIR}/bin
		/usr/bin
		/usr/local/bin
	)

	if(XSD_EXECUTABLE)
		SET(XSD_FOUND TRUE)
	else(XSD_EXECUTABLE)
		SET(XSD_EXECUTABLE "xsd-NOTFOUND" CACHE FILE "xsd executable path")
	endif(XSD_EXECUTABLE)
	
	find_path(XSD_INCLUDE_DIR NAMES xsd 
	    PATHS
		${XSD_DIR}/include
		/usr/include
		/usr/local/include
	)

	if(XSD_INCLUDE_DIR)
		SET(XSD_FOUND TRUE)
	else(XSD_INCLUDE_DIR)
		SET(XSD_INCLUDE_DIR "xsd-include-NOTFOUND" CACHE PATH "xsd include path")
	endif(XSD_INCLUDE_DIR)
endif(XSD_INCLUDE_DIR)

FUNCTION(XSD_EXTRACT_OPTIONS _xsd_files _xsd_options)
	foreach(current_arg ${ARGN})
		IF(${current_arg} STREQUAL "OPTIONS")
			SET(_XSD_DOING_OPTIONS TRUE)
		else(${current_arg} STREQUAL "OPTIONS")
			if(_XSD_DOING_OPTIONS)
				SET(_xsd_options_p ${_xsd_options_p} ${current_arg})
			else(_XSD_DOING_OPTIONS)
				SET(_xsd_files_p ${_xsd_files_p} ${current_arg})
			endif(_XSD_DOING_OPTIONS)
		endif(${current_arg} STREQUAL "OPTIONS")
	endforeach(current_arg)
	SET(${_xsd_files} ${_xsd_files_p} PARENT_SCOPE)
	SET(${_xsd_options} ${_xsd_options_p} PARENT_SCOPE)
ENDFUNCTION(XSD_EXTRACT_OPTIONS)


FUNCTION(WRAP_XSD XSD_SRCS XSD_INCLUDES OUT_PATH)
	SET(OUTPUT_DIR  ${CMAKE_CURRENT_BINARY_DIR}/src/xsd)
	FILE(MAKE_DIRECTORY ${OUTPUT_DIR})
	SET(${XSD_INCLUDES} ${OUTPUT_DIR} PARENT_SCOPE)
	XSD_EXTRACT_OPTIONS(xsd_files xsd_options ${ARGN})
	FOREACH(it ${xsd_files})
		STRING(REGEX REPLACE ".*/" "" BARE_XSD "${it}" )
		STRING(REGEX REPLACE ".xsd" ".cpp" SOURCE "${BARE_XSD}" )
		STRING(REGEX REPLACE ".xsd" ".h" HEADER "${BARE_XSD}" )
		CONFIGURE_FILE(${it} ${OUT_PATH}/${BARE_XSD} COPY_ONLY)
		SET(SOURCE ${OUTPUT_DIR}/${SOURCE})
		SET(HEADER ${OUTPUT_DIR}/${HEADER})
		ADD_CUSTOM_COMMAND(OUTPUT ${SOURCE} ${HEADER}
				COMMAND ${XSD_EXECUTABLE} ${xsd_options} "--output-dir" ${OUTPUT_DIR} ${OUT_PATH}/${BARE_XSD}
				DEPENDS ${it}
				VERBATIM
		)
		set_source_files_properties(${HEADER} PROPERTIES GENERATED TRUE)
		set_source_files_properties(${SOURCE} PROPERTIES GENERATED TRUE)
		SET(_XSD_SRCS ${_XSD_SRCS} ${SOURCE} ${HEADER})
	ENDFOREACH(it)
	SET(${XSD_SRCS} ${_XSD_SRCS} PARENT_SCOPE)
ENDFUNCTION(WRAP_XSD)



More information about the xsd-users mailing list