diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..0fc3f7c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,106 @@ +# https://cmake.org/cmake/help/v3.10/command/project.html +project(helloworld + DESCRIPTION "Print Hello World toolkit" + LANGUAGES C) +# https://cmake.org/cmake/help/v3.10/command/cmake_minimum_required.html +cmake_minimum_required(VERSION 2.8) + +# https://cmake.org/cmake/help/v3.10/command/option.html +# will show up in "cmake -LAH" +option(WANT_CMAKE_TO_FAIL "this option will make cmake fail") +option(WANT_TESTS "this option enable tests" ON) +option(WANT_COVERAGE "this option enable coverage" ON) + +# https://cmake.org/cmake/help/v3.10/command/if.html +if(WANT_CMAKE_TO_FAIL) + # https://cmake.org/cmake/help/v3.10/command/message.html + message(FATAL_ERROR "CMake failed due to WANT_CMAKE_FAIL option") +endif() + +if(NOT WANT_TESTS) + # https://cmake.org/cmake/help/v3.10/command/set.html + set(WANT_COVERAGE OFF) +endif() + +if(CMAKE_BUILD_TYPE MATCHES DEBUG) + set(WANT_COVERAGE OFF) +endif() + +if(WANT_COVERAGE) + # https://cmake.org/cmake/help/v3.10/command/list.html + list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake-modules") + # https://cmake.org/cmake/help/v3.10/command/include.html + include(CodeCoverage) + # The following line come from cmake-modules/CodeCoverage.cmake + append_coverage_compiler_flags() +endif() + +# https://cmake.org/cmake/help/v3.10/command/add_executable.html +add_executable(main main.c) +# https://cmake.org/cmake/help/v3.10/command/target_include_directories.html +target_include_directories(main PRIVATE include) + +add_executable(main2 main2.c simple_print_hello_world.c include/simple_print_hello_world.h) +target_include_directories(main2 PRIVATE include) + +# https://cmake.org/cmake/help/v3.10/command/add_library.html +add_library(phw print_hello_world.c include/print_hello_world.h include/print_hello_world_defines.h) +# https://cmake.org/cmake/help/v3.10/command/target_include_directories.html +target_include_directories(phw PUBLIC include) +# https://cmake.org/cmake/help/v3.10/command/set_target_properties.html +# https://cmake.org/cmake/help/v3.10/manual/cmake-properties.7.html#properties-on-targets +# https://cmake.org/cmake/help/v3.10/prop_tgt/PUBLIC_HEADER.html#prop_tgt:PUBLIC_HEADER +set_target_properties(phw PROPERTIES + PUBLIC_HEADER "include/print_hello_world.h;include/print_hello_world_defines.h") + +add_executable(main3 main3.c) +# https://cmake.org/cmake/help/v3.10/command/target_link_libraries.html +target_link_libraries(main3 phw) +# https://cmake.org/cmake/help/v3.10/command/target_include_directories.html +# https://cmake.org/cmake/help/v3.10/manual/cmake-generator-expressions.7.html#manual:cmake-generator-expressions(7) +# https://cmake.org/cmake/help/v3.10/prop_tgt/INTERFACE_INCLUDE_DIRECTORIES.html +target_include_directories(main3 PRIVATE $) + +# https://cmake.org/cmake/help/v3.10/command/add_library.html +add_library(phw-shared SHARED print_hello_world.c include/print_hello_world.h include/print_hello_world_defines.h) +target_include_directories(phw-shared PUBLIC include) +set_target_properties(phw-shared PROPERTIES + PUBLIC_HEADER "include/print_hello_world.h;include/print_hello_world_defines.h") + +add_executable(main4 main3.c) +target_link_libraries(main4 phw-shared) +target_include_directories(main4 PRIVATE $) + +# https://cmake.org/cmake/help/v3.10/command/install.html +install(TARGETS main main2 main3 main4 phw phw-shared + RUNTIME DESTINATION bin + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + PUBLIC_HEADER DESTINATION include) + +if(WANT_TESTS) + # https://cmake.org/cmake/help/v3.10/command/enable_testing.html + enable_testing() + + # https://cmake.org/cmake/help/v3.10/command/add_test.html + add_test(test-main ${TARGET_SYSTEM_EMULATOR} main) + # https://cmake.org/cmake/help/v3.10/command/set_tests_properties.html + set_tests_properties(test-main PROPERTIES PASS_REGULAR_EXPRESSION "^Hello world!\n$") + + add_test(test-main2 ${TARGET_SYSTEM_EMULATOR} main2) + set_tests_properties(test-main2 PROPERTIES PASS_REGULAR_EXPRESSION "^Hello world!\n$") + + add_test(test-main3 ${TARGET_SYSTEM_EMULATOR} main3) + set_tests_properties(test-main3 PROPERTIES PASS_REGULAR_EXPRESSION "^Hello world!\n$") + + add_test(test-main4 ${TARGET_SYSTEM_EMULATOR} main4) + set_tests_properties(test-main4 PROPERTIES PASS_REGULAR_EXPRESSION "^Hello world!\n$") +endif() + +if(WANT_COVERAGE) + setup_target_for_coverage( + NAME coverage + EXECUTABLE ctest -j ${PROCESSOR_COUNT} + DEPENDENCIES main main2 main3 main4 + ) +endif() diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1327bc1 --- /dev/null +++ b/Makefile @@ -0,0 +1,98 @@ +# Standard .c -> .o compilation command +%.o: %.c + $(CC) -c $(<) -o $(@) -Iinclude + +STATIC_LINKER := $(AR) qc +DYNAMIC_LINKER := $(CC) -shared + +# Default target +all: main main2 main3 main4 libphw.a libphw-shared.so + +# Compile main - BEGIN + +MAIN_SOURCES := main.c +MAIN_OBJECTS := $(MAIN_SOURCES:.c=.o) + +main: $(MAIN_OBJECTS) $(MAIN_SOURCES) + $(CC) $(MAIN_OBJECTS) -o $(@) + +clean:: + $(RM) -f main $(MAIN_OBJECTS) + +# Compile main - END + +# Compile main2 - BEGIN + +MAIN2_SOURCES := main2.c simple_print_hello_world.c include/simple_print_hello_world.h +MAIN2_OBJECTS := $(filter %.o,$(MAIN2_SOURCES:.c=.o)) + +main2: $(MAIN2_OBJECTS) $(MAIN2_SOURCES) + $(CC) $(MAIN2_OBJECTS) -o $(@) + +clean:: + $(RM) -f main2 $(MAIN2_OBJECTS) + +# Compile main2 - END + +# Compile libphw.a - BEGIN + +PHW_SOURCES := print_hello_world.c include/print_hello_world.h +PHW_OBJECTS := $(filter %.o,$(PHW_SOURCES:.c=.o)) + +libphw.a: $(PHW_OBJECTS) $(PHW_SOURCES) + $(STATIC_LINKER) $(@) $(PHW_OBJECTS) + +clean:: + $(RM) -f libphw.a $(PHW_OBJECTS) + +# Compile libphw.a - END + +# Compile main3 - BEGIN + +MAIN3_SOURCES := main3.c include/simple_print_hello_world.h +MAIN3_LIBS := libphw.a +MAIN3_OBJECTS := $(filter %.o,$(MAIN3_SOURCES:.c=.o)) + +main3: $(MAIN3_OBJECTS) $(MAIN3_SOURCES) $(MAIN3_LIBS) + $(CC) $(MAIN3_OBJECTS) $(MAIN3_LIBS) -o $(@) + +clean:: + $(RM) -f main3 $(MAIN3_OBJECTS) + +# Compile main3 - END + +# Compile libphw-shared.so - BEGIN + +PHW_SHARED_SOURCES := print_hello_world.c include/print_hello_world.h +PHW_SHARED_OBJECTS := $(addprefix shared-objs-, $(filter %.o,$(PHW_SHARED_SOURCES:.c=.o))) + +# Shared objects .c -> .o compilation command +shared-objs-%.o: %.c + $(CC) -c $(<) -fPIC -o $(@) -Iinclude + +libphw-shared.so: $(PHW_SHARED_OBJECTS) $(PHW_SHARED_SOURCES) + $(DYNAMIC_LINKER) -o $(@) $(PHW_SHARED_OBJECTS) + +clean:: + $(RM) -f libphw-shared.so $(PHW_SHARED_OBJECTS) + +# Compile libphw-shared.so - END + +# Compile main4 - BEGIN + +MAIN4_SOURCES := main3.c include/simple_print_hello_world.h +MAIN4_LIBS := libphw-shared.so +MAIN4_OBJECTS := $(filter %.o,$(MAIN4_SOURCES:.c=.o)) + +main4: $(MAIN3_OBJECTS) $(MAIN4_SOURCES) $(MAIN4_LIBS) + $(CC) $(MAIN4_OBJECTS) $(MAIN4_LIBS) -o $(@) + +clean:: + $(RM) -f main4 $(MAIN4_OBJECTS) + +# Compile main4 - END + +# Makefile behaviour targets + +.PHONY: clean all +.DELETE_ON_ERRORS: diff --git a/README.md b/README.md new file mode 100644 index 0000000..e282238 --- /dev/null +++ b/README.md @@ -0,0 +1,219 @@ +Build +===== + +Build using scripts +------------------- + +In order to build a software with scripts, you just have to add build +instructions to a script. Writing a script is easy and you can do almost +any kind of processing. However it is not easy to make a good, portable, +customizable build processing. It is also difficult to properly implement +incremental building, that avoid rebuilding everything if not needed. + +### Compile ### + +``` +./build.sh +``` + +### Clean ### + +``` +./build.sh clean +``` + +### Pros ### + +* Easy to write +* Can do almost everything + +### Cons ### + +* Non standard +* No incremental build +* No build-time customization + +Build using Makefile +-------------------- + +### Compile ### + +``` +make -j +``` + +### Clean ### + +``` +make -j clean +``` + +Cross-build using Makefile +-------------------------- + +### Compile ### + +``` +make -j CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar +``` + +### Clean ### + +``` +make -j clean +``` + +Build using CMake + Makefile +---------------------------- + +### Configure ### + +``` +mkdir -p build +cd build +cmake .. -DCMAKE_INSTALL_PREFIX=install -DCMAKE_VERBOSE_MAKEFILE=ON +``` + + +### Compile ### + +``` +make -j +``` + +### Install ### + +``` +make -j install +``` + +### Clean ### + +``` +make -j clean +``` + +Build using CMake + Ninja +---------------------------- + +### Configure ### + +``` +mkdir -p build-ninja +cd build-ninja +cmake .. -GNinja -DCMAKE_INSTALL_PREFIX=install +``` + + +### Compile ### + +``` +ninja +``` + +### Install ### + +``` +ninja install +``` + +### Clean ### + +``` +ninja clean +``` + +Cross-build using CMake + Makefile +---------------------------------- + +### Configure ### + +``` +mkdir -p build-mingw32 +cd build-mingw32 +cmake .. -DCMAKE_INSTALL_PREFIX=install -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_TOOLCHAIN_FILE=../toolchains/Toolchain-mingw32.cmake +``` + + +### Compile ### + +``` +make -j +``` + +### Install ### + +``` +make -j install +``` + +### Clean ### + +``` +make -j clean +``` + +Build + Coverage test using CMake + Makefile +-------------------------------------------- + +### Configure ### + +``` +mkdir -p build +cd build +cmake .. -DCMAKE_INSTALL_PREFIX=install -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=Debug +``` + + +### Compile ### + +``` +make -j +``` + +### Install ### + +``` +make -j install +``` + +### Clean ### + +``` +make -j clean +``` + +### Test ### + +``` +make -j test +``` + +### Coverage ### + +``` +make -j coverage +``` + +Run +=== + +Run native programs +------------------- + +``` +./main +./main2 +./main3 +LD_LIBRARY_PATH=. ./main4 +``` + +Run Windows programs on Linux +----------------------------- + +``` +wine ./main +wine ./main2 +wine ./main3 +wine ./main4 +``` diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..d0fd1a0 --- /dev/null +++ b/build.sh @@ -0,0 +1,36 @@ +#!/bin/sh + +set -e + +case "$1" in + "clean") + rm -f main + + rm -f main2 simple_print_hello_world.o + + rm -f libphw.a print_hello_world.o + rm -f main3 + + rm -f libphw-shared.so + rm -fr shared-objs-print_hello_world.o + rm -f main4 + + ;; + *) + # Single file program + gcc main.c -o main + + # Multiple files program + gcc -Iinclude main2.c simple_print_hello_world.c -o main2 + + # Static library + program + gcc -Iinclude -c print_hello_world.c -o print_hello_world.o + ar qc libphw.a print_hello_world.o + gcc -Iinclude main3.c -L. -lphw -o main3 + + # Shared library + program + gcc -Iinclude -c print_hello_world.c -fPIC -o shared-objs-print_hello_world.o + gcc -shared shared-objs-print_hello_world.o -o libphw-shared.so + gcc -Iinclude main3.c -L. -lphw-shared -o main4 + ;; +esac diff --git a/cmake-modules/CodeCoverage.cmake b/cmake-modules/CodeCoverage.cmake new file mode 100644 index 0000000..0f90f8d --- /dev/null +++ b/cmake-modules/CodeCoverage.cmake @@ -0,0 +1,272 @@ +# Copyright (c) 2012 - 2017, Lars Bilke +# All rights reserved. +# +# 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 above copyright notice, this +# list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors +# may be used to endorse or promote products derived from this software without +# specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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. +# +# CHANGES: +# +# 2012-01-31, Lars Bilke +# - Enable Code Coverage +# +# 2013-09-17, Joakim Söderberg +# - Added support for Clang. +# - Some additional usage instructions. +# +# 2016-02-03, Lars Bilke +# - Refactored functions to use named parameters +# +# 2017-06-02, Lars Bilke +# - Merged with modified version from github.com/ufz/ogs +# +# +# USAGE: +# +# 1. Copy this file into your cmake modules path. +# +# 2. Add the following line to your CMakeLists.txt: +# include(CodeCoverage) +# +# 3. Append necessary compiler flags: +# APPEND_COVERAGE_COMPILER_FLAGS() +# +# 4. If you need to exclude additional directories from the report, specify them +# using the COVERAGE_EXCLUDES variable before calling SETUP_TARGET_FOR_COVERAGE. +# Example: +# set(COVERAGE_EXCLUDES 'dir1/*' 'dir2/*') +# +# 5. Use the functions described below to create a custom make target which +# runs your test executable and produces a code coverage report. +# +# 6. Build a Debug build: +# cmake -DCMAKE_BUILD_TYPE=Debug .. +# make +# make my_coverage_target +# + +include(CMakeParseArguments) + +# Check prereqs + +if(NOT CMAKE_GCOV) + set(CMAKE_GCOV gcov) +endif() +mark_as_advanced(CMAKE_GCOV) + +find_program( GCOV_PATH ${CMAKE_GCOV} ) +find_program( LCOV_PATH lcov ) +find_program( GENHTML_PATH genhtml ) +find_program( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/scripts/test) +find_program( SIMPLE_PYTHON_EXECUTABLE python ) + +if(NOT GCOV_PATH) + message(FATAL_ERROR "gcov not found! Aborting...") +endif() # NOT GCOV_PATH + +if(CMAKE_C_COMPILER_ID) + if("${CMAKE_C_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang") + if("${CMAKE_C_COMPILER_VERSION}" VERSION_LESS 3) + message(FATAL_ERROR "Clang version must be 3.0.0 or greater! Aborting...") + endif() + elseif(NOT CMAKE_COMPILER_IS_GNUCC) + message(FATAL_ERROR "Compiler is not GNU gcc! Aborting...") + endif() +endif() + +if(CMAKE_CXX_COMPILER_ID) + if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang") + if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS 3) + message(FATAL_ERROR "Clang version must be 3.0.0 or greater! Aborting...") + endif() + elseif(NOT CMAKE_COMPILER_IS_GNUCXX) + message(FATAL_ERROR "Compiler is not GNU gcc! Aborting...") + endif() +endif() + +set(COVERAGE_COMPILER_FLAGS "-g -O0 --coverage -fprofile-arcs -ftest-coverage" + CACHE INTERNAL "") + +if(CMAKE_CXX_COMPILER_ID) + set(CMAKE_CXX_FLAGS_COVERAGE + ${COVERAGE_COMPILER_FLAGS} + CACHE STRING "Flags used by the C++ compiler during coverage builds." + FORCE ) +endif() + +if(CMAKE_C_COMPILER_ID) + set(CMAKE_C_FLAGS_COVERAGE + ${COVERAGE_COMPILER_FLAGS} + CACHE STRING "Flags used by the C compiler during coverage builds." + FORCE ) +endif() + +set(CMAKE_EXE_LINKER_FLAGS_COVERAGE + "" + CACHE STRING "Flags used for linking binaries during coverage builds." + FORCE ) +set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE + "" + CACHE STRING "Flags used by the shared libraries linker during coverage builds." + FORCE ) + +if(CMAKE_C_COMPILER_ID) + mark_as_advanced(CMAKE_C_FLAGS_COVERAGE) +endif() +if(CMAKE_CXX_COMPILER_ID) + mark_as_advanced(CMAKE_CXX_FLAGS_COVERAGE) +endif() + +mark_as_advanced( + CMAKE_EXE_LINKER_FLAGS_COVERAGE + CMAKE_SHARED_LINKER_FLAGS_COVERAGE ) + +if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") + message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading") +endif() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug" + +if(CMAKE_C_COMPILER_ID STREQUAL "GNU") + link_libraries(gcov) +else() + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage") +endif() + +# Defines a target for running and collection code coverage information +# Builds dependencies, runs the given executable and outputs reports. +# NOTE! The executable should always have a ZERO as exit code otherwise +# the coverage generation will not complete. +# +# SETUP_TARGET_FOR_COVERAGE( +# NAME testrunner_coverage # New target name +# EXECUTABLE testrunner -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR +# DEPENDENCIES testrunner # Dependencies to build first +# ) +function(SETUP_TARGET_FOR_COVERAGE) + + set(options NONE) + set(oneValueArgs NAME) + set(multiValueArgs EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES) + cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + if(NOT LCOV_PATH) + message(FATAL_ERROR "lcov not found! Aborting...") + endif() # NOT LCOV_PATH + + if(NOT GENHTML_PATH) + message(FATAL_ERROR "genhtml not found! Aborting...") + endif() # NOT GENHTML_PATH + + # Setup target + add_custom_target(${Coverage_NAME} + + # Cleanup lcov + COMMAND ${LCOV_PATH} --directory . --zerocounters + # Create baseline to make sure untouched files show up in the report + COMMAND ${LCOV_PATH} -c -i -d . -o ${Coverage_NAME}.base + + # Run tests + COMMAND ${Coverage_EXECUTABLE} + + # Capturing lcov counters and generating report + COMMAND ${LCOV_PATH} --directory . --capture --output-file ${Coverage_NAME}.info + # add baseline counters + COMMAND ${LCOV_PATH} -a ${Coverage_NAME}.base -a ${Coverage_NAME}.info --output-file ${Coverage_NAME}.total + COMMAND ${LCOV_PATH} --remove ${Coverage_NAME}.total ${COVERAGE_EXCLUDES} --output-file ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned + COMMAND ${GENHTML_PATH} -o ${Coverage_NAME} ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned + COMMAND ${CMAKE_COMMAND} -E remove ${Coverage_NAME}.base ${Coverage_NAME}.info ${Coverage_NAME}.total ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned + + WORKING_DIRECTORY ${PROJECT_BINARY_DIR} + DEPENDS ${Coverage_DEPENDENCIES} + COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report." + ) + + # Show info where to find the report + add_custom_command(TARGET ${Coverage_NAME} POST_BUILD + COMMAND ; + COMMENT "Open ./${Coverage_NAME}/index.html in your browser to view the coverage report." + ) + +endfunction() # SETUP_TARGET_FOR_COVERAGE + +# Defines a target for running and collection code coverage information +# Builds dependencies, runs the given executable and outputs reports. +# NOTE! The executable should always have a ZERO as exit code otherwise +# the coverage generation will not complete. +# +# SETUP_TARGET_FOR_COVERAGE_COBERTURA( +# NAME ctest_coverage # New target name +# EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR +# DEPENDENCIES executable_target # Dependencies to build first +# ) +function(SETUP_TARGET_FOR_COVERAGE_COBERTURA) + + set(options NONE) + set(oneValueArgs NAME) + set(multiValueArgs EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES) + cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + if(NOT SIMPLE_PYTHON_EXECUTABLE) + message(FATAL_ERROR "python not found! Aborting...") + endif() # NOT SIMPLE_PYTHON_EXECUTABLE + + if(NOT GCOVR_PATH) + message(FATAL_ERROR "gcovr not found! Aborting...") + endif() # NOT GCOVR_PATH + + # Combine excludes to several -e arguments + set(COBERTURA_EXCLUDES "") + foreach(EXCLUDE ${COVERAGE_EXCLUDES}) + set(COBERTURA_EXCLUDES "-e ${EXCLUDE} ${COBERTURA_EXCLUDES}") + endforeach() + + add_custom_target(${Coverage_NAME} + + # Run tests + ${Coverage_EXECUTABLE} + + # Running gcovr + COMMAND ${GCOVR_PATH} -x -r ${CMAKE_SOURCE_DIR} ${COBERTURA_EXCLUDES} + -o ${Coverage_NAME}.xml + WORKING_DIRECTORY ${PROJECT_BINARY_DIR} + DEPENDS ${Coverage_DEPENDENCIES} + COMMENT "Running gcovr to produce Cobertura code coverage report." + ) + + # Show info where to find the report + add_custom_command(TARGET ${Coverage_NAME} POST_BUILD + COMMAND ; + COMMENT "Cobertura code coverage report saved in ${Coverage_NAME}.xml." + ) + +endfunction() # SETUP_TARGET_FOR_COVERAGE_COBERTURA + +function(APPEND_COVERAGE_COMPILER_FLAGS) + if(CMAKE_C_COMPILER_ID) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE) + endif() + if(CMAKE_CXX_COMPILER_ID) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE) + endif() + message(STATUS "Appending code coverage compiler flags: ${COVERAGE_COMPILER_FLAGS}") +endfunction() # APPEND_COVERAGE_COMPILER_FLAGS diff --git a/include/print_hello_world.h b/include/print_hello_world.h new file mode 100644 index 0000000..b6516bf --- /dev/null +++ b/include/print_hello_world.h @@ -0,0 +1,12 @@ +#ifndef _PRINT_HELLO_WORLD_H +#define _PRINT_HELLO_WORLD_H + +#include "print_hello_world_defines.h" + +PHW_BEGIN_C_DECLS + +PHW_API void print_hello_world(); + +PHW_END_C_DECLS + +#endif diff --git a/include/print_hello_world_defines.h b/include/print_hello_world_defines.h new file mode 100644 index 0000000..b1da404 --- /dev/null +++ b/include/print_hello_world_defines.h @@ -0,0 +1,42 @@ +#ifndef _PHW_DEFINES_H_ +#define _PHW_DEFINES_H_ + +#undef PHW_BEGIN_C_DECLS +#undef PHW_END_C_DECLS +#ifdef __cplusplus +# define PHW_BEGIN_C_DECLS extern "C" { +# define PHW_END_C_DECLS } +#else /* !__cplusplus */ +# define PHW_BEGIN_C_DECLS +# define PHW_END_C_DECLS +#endif /* __cplusplus */ + +#if defined _WIN32 || defined __CYGWIN__ +# define PHW_HELPER_DLL_IMPORT __declspec(dllimport) +# define PHW_HELPER_DLL_EXPORT __declspec(dllexport) +# define PHW_HELPER_DLL_LOCAL +#else +# if __GNUC__ >= 4 +# define PHW_HELPER_DLL_IMPORT __attribute__ ((visibility("default"))) +# define PHW_HELPER_DLL_EXPORT __attribute__ ((visibility("default"))) +# define PHW_HELPER_DLL_LOCAL __attribute__ ((visibility("hidden"))) +# else +# define PHW_HELPER_DLL_IMPORT +# define PHW_HELPER_DLL_EXPORT +# define PHW_HELPER_DLL_LOCAL +# endif +#endif + +#ifdef PHW_LIBRARIES_EXPORTS +# ifdef PHW_SRC +# define PHW_API PHW_HELPER_DLL_EXPORT +# else +# define PHW_API PHW_HELPER_DLL_IMPORT +# endif +# define PHW_LOCAL PHW_HELPER_DLL_LOCAL +#else +# define PHW_API +# define PHW_LOCAL +#endif + +#endif diff --git a/include/simple_print_hello_world.h b/include/simple_print_hello_world.h new file mode 100644 index 0000000..a642553 --- /dev/null +++ b/include/simple_print_hello_world.h @@ -0,0 +1,6 @@ +#ifndef _SIMPLE_PRINT_HELLO_WORLD_H +#define _SIMPLE_PRINT_HELLO_WORLD_H + +void simple_print_hello_world(); + +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..5439d9b --- /dev/null +++ b/main.c @@ -0,0 +1,6 @@ +#include + +int main(int argc, char** argv) { + printf("Hello world!\n"); + return 0; +} diff --git a/main2.c b/main2.c new file mode 100644 index 0000000..4a88d51 --- /dev/null +++ b/main2.c @@ -0,0 +1,7 @@ +#include +#include "simple_print_hello_world.h" + +int main(int argc, char** argv) { + simple_print_hello_world(); + return 0; +} diff --git a/main3.c b/main3.c new file mode 100644 index 0000000..703dcc3 --- /dev/null +++ b/main3.c @@ -0,0 +1,7 @@ +#include +#include "print_hello_world.h" + +int main(int argc, char** argv) { + print_hello_world(); + return 0; +} diff --git a/print_hello_world.c b/print_hello_world.c new file mode 100644 index 0000000..66605af --- /dev/null +++ b/print_hello_world.c @@ -0,0 +1,11 @@ +#include "print_hello_world.h" +#include + +void print_hello_world() { + printf("Hello world!\n"); + if(stderr!=stdout) { + printf(""); + } else { + printf(""); + } +} diff --git a/simple_print_hello_world.c b/simple_print_hello_world.c new file mode 100644 index 0000000..4b2953d --- /dev/null +++ b/simple_print_hello_world.c @@ -0,0 +1,6 @@ +#include "simple_print_hello_world.h" +#include + +void simple_print_hello_world() { + printf("Hello world!\n"); +} diff --git a/toolchains/Toolchain-mingw32.cmake b/toolchains/Toolchain-mingw32.cmake new file mode 100644 index 0000000..1b09fc3 --- /dev/null +++ b/toolchains/Toolchain-mingw32.cmake @@ -0,0 +1,27 @@ +set(CMAKE_SYSTEM_NAME Windows) +set(CMAKE_SYSTEM_PROCESSOR i686) +set(CMAKE_C_COMPILER i686-w64-mingw32-gcc) +set(CMAKE_CXX_COMPILER i686-w64-mingw32-g++) +set(CMAKE_DLLTOOL i686-w64-mingw32-dlltool) +set(CMAKE_GCOV i686-w64-mingw32-gcov) +set(CMAKE_FIND_ROOT_PATH /usr/i686-w64-mingw32) + +# Adjust the default behaviour of the FIND_XXX() commands: +# search headers and libraries in the target environment, search +# programs in the host environment +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) + +# Tell pkg-config not to look at the target environment's .pc files. +# Setting PKG_CONFIG_LIBDIR sets the default search directory, but we have to +# set PKG_CONFIG_PATH as well to prevent pkg-config falling back to the host's +# path. +set(ENV{PKG_CONFIG_LIBDIR} ${CMAKE_FIND_ROOT_PATH}/lib/pkgconfig) +set(ENV{PKG_CONFIG_PATH} ${CMAKE_FIND_ROOT_PATH}/lib/pkgconfig) + +set(INSTALL_PREFIX ${CMAKE_FIND_ROOT_PATH}) +set(ENV{MINGDIR} ${CMAKE_FIND_ROOT_PATH}) + +# Set a command to run executables +set(TARGET_SYSTEM_EMULATOR wine)