From: brarcher Date: Tue, 4 Feb 2014 00:35:43 +0000 (+0000) Subject: example: add CMake based example X-Git-Tag: 0.10.0~128 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=aa4a0f042667c9f1610196efc0611ceea16049a3;p=check example: add CMake based example This example does the same thing, just with CMake instead of autotools. Note that it uses a FindCheck.cmake file to locate libcheck on the system using pkg-config. This file is originally from the opensync project. Attribution and info on how to find the original are included. git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@1089 64e312b2-a51f-0410-8e61-82d0ca0eb02a --- diff --git a/doc/Makefile.am b/doc/Makefile.am index c618049..02b8ed0 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -99,6 +99,13 @@ example_tests_docs = example/tests/Makefile.am \ example/tests/check_money.6.c \ example/tests/check_money.7.c +example_cmake = example/CMakeLists.txt \ + example/src/CMakeLists.txt \ + example/tests/CMakeLists.txt \ + example/cmake/config.h.in \ + example/cmake/COPYING-CMAKE-SCRIPTS.txt \ + example/cmake/FindCheck.cmake + ## what to clean CLEANFILES = check.info *~ *.diff @@ -109,7 +116,8 @@ clean-local: EXTRA_DIST = $(example_docs) \ $(example_src_docs) \ - $(example_tests_docs) + $(example_tests_docs) \ + $(example_cmake) ## what to install diff --git a/doc/example/CMakeLists.txt b/doc/example/CMakeLists.txt new file mode 100644 index 0000000..d49ce7b --- /dev/null +++ b/doc/example/CMakeLists.txt @@ -0,0 +1,80 @@ +project(money C) + +cmake_minimum_required(VERSION 2.8 FATAL_ERROR) +set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") + +############################################################################### +# Set build features +set(CMAKE_BUILD_TYPE Debug) + +############################################################################### +include(CheckCSourceCompiles) +include(CheckCSourceRuns) +include(CheckFunctionExists) +include(CheckIncludeFile) +include(CheckIncludeFiles) +include(CheckLibraryExists) +include(CheckSymbolExists) +include(CheckTypeSize) + +############################################################################### +# Check headers +set(INCLUDES "") +macro(ck_check_include_file header var) + check_include_files("${INCLUDES};${header}" ${var}) + if(${var}) + set(INCLUDES ${INCLUDES} ${header}) + endif(${var}) +endmacro(ck_check_include_file) + +ck_check_include_file("stdlib.h" HAVE_STDLIB_H) + +############################################################################### +# Check functions +# (Nothing to check for the money example) + +############################################################################### +# Check defines +# (Nothing to check for the money example) + +############################################################################### +# Check struct members +# (Nothing to check for the money example) + +############################################################################### +# Check for integer types +# (The following are used in check.h. Regardless if they are used in +# the project, they will need to be checked in order to use Check). +check_type_size(intmax_t INTMAX_T) +check_type_size(uintmax_t UINTMAX_T) + +check_type_size(pid_t PID_T) +if(NOT HAVE_PID_T) + if(WIN32) + set(pid_t "int") + else(WIN32) + MESSAGE(FATAL_ERROR "pid_t doesn't exist on this platform?") + endif(WIN32) +endif(NOT HAVE_PID_T) + +############################################################################### +# Check libraries + +############################################################################### +# Generate "config.h" from "cmake/config.h.cmake" +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.h.in + ${CMAKE_CURRENT_BINARY_DIR}/config.h) +include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR}) +add_definitions(-DHAVE_CONFIG_H) +set(CONFIG_HEADER ${CMAKE_CURRENT_BINARY_DIR}/config.h) + +############################################################################### +# Subdirectories +add_subdirectory(src) +add_subdirectory(tests) + +############################################################################### +# Unit tests +enable_testing() +add_test(NAME check_money COMMAND check_money) + diff --git a/doc/example/README b/doc/example/README index c894e9d..7791921 100644 --- a/doc/example/README +++ b/doc/example/README @@ -1,5 +1,8 @@ This is the "money example" from the Check tutorial. +This demonstrates using Check along with one of two build systems: +autotools and CMake. + ======================== Autotools: @@ -36,3 +39,45 @@ files: |--- Makefile.am Please send bug reports to check-devel AT lists.sourceforge.net. + +======================== +CMake: + +You need the following programs installed on your system: + -- CMake 2.8 + -- Check 0.9.9 + -- pkg-config 0.26 + +Somewhat earlier versions of these programs might work. + +Then, do as follows for a Unix-compatible shell: + +$ cmake . +$ make +$ make test + +or the following for MSVC: + +$ cmake -G "NMake Makefiles" . +$ nmake +$ nmake test + +Don't do "make install" or "nmake install" unless you want to install the money example. + +money.c and money.h are built as a library. src/main.c:main() is a +client of libmoney.la, just as tests/check_money.c:main() is a client +of libmoney.la + +To use the CMake example in another project, start with the following files: + + example + |--- CMakeFiles.txt + |--- cmake + | |--- config.h.in + | |--- FindCheck.cmake + |--- src + | |--- CMakeFiles.txt + |--- tests + |--- CMakeFiles.txt + +Please send bug reports to check-devel AT lists.sourceforge.net. diff --git a/doc/example/cmake/COPYING-CMAKE-SCRIPTS.txt b/doc/example/cmake/COPYING-CMAKE-SCRIPTS.txt new file mode 100644 index 0000000..53b6b71 --- /dev/null +++ b/doc/example/cmake/COPYING-CMAKE-SCRIPTS.txt @@ -0,0 +1,22 @@ +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/doc/example/cmake/FindCheck.cmake b/doc/example/cmake/FindCheck.cmake new file mode 100644 index 0000000..a7cd11a --- /dev/null +++ b/doc/example/cmake/FindCheck.cmake @@ -0,0 +1,50 @@ +# - Try to find the CHECK libraries +# Once done this will define +# +# CHECK_FOUND - system has check +# CHECK_INCLUDE_DIR - the check include directory +# CHECK_LIBRARIES - check library +# +# This configuration file for finding libcheck is originally from +# the opensync project. The originally was downloaded from here: +# opensync.org/browser/branches/3rd-party-cmake-modules/modules/FindCheck.cmake +# +# Copyright (c) 2007 Daniel Gollub +# Copyright (c) 2007 Bjoern Ricks +# +# Redistribution and use is allowed according to the terms of the New +# BSD license. +# For details see the accompanying COPYING-CMAKE-SCRIPTS file. + + +INCLUDE( FindPkgConfig ) + +# Take care about check.pc settings +PKG_SEARCH_MODULE( CHECK check ) + +# Look for CHECK include dir and libraries +IF( NOT CHECK_FOUND ) + + FIND_PATH( CHECK_INCLUDE_DIR check.h ) + + FIND_LIBRARY( CHECK_LIBRARIES NAMES check ) + + IF ( CHECK_INCLUDE_DIR AND CHECK_LIBRARIES ) + SET( CHECK_FOUND 1 ) + IF ( NOT Check_FIND_QUIETLY ) + MESSAGE ( STATUS "Found CHECK: ${CHECK_LIBRARIES}" ) + ENDIF ( NOT Check_FIND_QUIETLY ) + ELSE ( CHECK_INCLUDE_DIR AND CHECK_LIBRARIES ) + IF ( Check_FIND_REQUIRED ) + MESSAGE( FATAL_ERROR "Could NOT find CHECK" ) + ELSE ( Check_FIND_REQUIRED ) + IF ( NOT Check_FIND_QUIETLY ) + MESSAGE( STATUS "Could NOT find CHECK" ) + ENDIF ( NOT Check_FIND_QUIETLY ) + ENDIF ( Check_FIND_REQUIRED ) + ENDIF ( CHECK_INCLUDE_DIR AND CHECK_LIBRARIES ) +ENDIF( NOT CHECK_FOUND ) + +# Hide advanced variables from CMake GUIs +MARK_AS_ADVANCED( CHECK_INCLUDE_DIR CHECK_LIBRARIES ) + diff --git a/doc/example/cmake/config.h.in b/doc/example/cmake/config.h.in new file mode 100644 index 0000000..2a0401e --- /dev/null +++ b/doc/example/cmake/config.h.in @@ -0,0 +1,13 @@ +/*-*- mode:C; -*- */ +/* config.h. Generated from build/cmake/config.h.in by cmake configure */ + +/* + * Ensure we have C99-style int64_t, etc, all defined. + */ + +/* First, we need to know if the system has already defined them. */ +#cmakedefine HAVE_INTMAX_T +#cmakedefine HAVE_UINTMAX_T + +/* Define to `int' if doesn't define. */ +#cmakedefine pid_t ${pid_t} diff --git a/doc/example/src/CMakeLists.txt b/doc/example/src/CMakeLists.txt new file mode 100644 index 0000000..b5e211e --- /dev/null +++ b/doc/example/src/CMakeLists.txt @@ -0,0 +1,24 @@ +set(LIB_SOURCES + money.c +) + +set(MAIN_SOURCES + main.c +) + +set(HEADERS + ${CONFIG_HEADER} + money.h +) + +add_library(money STATIC ${LIB_SOURCES} ${HEADERS}) + +add_executable(main ${HEADERS} ${MAIN_SOURCES}) +target_link_libraries(main money) + +install(TARGETS money + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib) + +install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/money.h DESTINATION include) diff --git a/doc/example/tests/CMakeLists.txt b/doc/example/tests/CMakeLists.txt new file mode 100644 index 0000000..28bd0f9 --- /dev/null +++ b/doc/example/tests/CMakeLists.txt @@ -0,0 +1,11 @@ +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src) + +find_package(Check REQUIRED) +include_directories(${CHECK_INCLUDE_DIRS}) + +set(TEST_SOURCES + check_money.c +) + +add_executable(check_money ${TEST_SOURCES}) +target_link_libraries(check_money money ${CHECK_LIBRARIES})