<commit_before>cmake_minimum_required (VERSION 2.8)
project(libnmea C)

set(LIBNMEA_MAJOR_VERSION 0)
set(LIBNMEA_MINOR_VERSION 1)
set(LIBNMEA_PATCH_VERSION 2)
set(LIBNMEA_VERSION
  ${LIBNMEA_MAJOR_VERSION}.${LIBNMEA_MINOR_VERSION}.${LIBNMEA_PATCH_VERSION})

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/")

#Define cmake install path
if(WIN32 AND NOT CYGWIN)
  set(DEF_INSTALL_CMAKE_DIR CMake)
else()
  set(DEF_INSTALL_CMAKE_DIR lib/cmake/${PROJECT_NAME})
endif()
set(INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH
  "Installation directory for CMake files")

# Make relative paths absolute (needed later on)
foreach(p LIB BIN INCLUDE CMAKE)
  set(var INSTALL_${p}_DIR)
  if(NOT IS_ABSOLUTE "${${var}}")
    set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
  endif()
endforeach()

option(NMEA_BUILD_STATIC_LIB "Build static libnmea" ON)
option(NMEA_BUILD_SHARED_LIB "Build shared libnmea" ON)
option(NMEA_BUILD_EXAMPLES "Build examples" ON)
option(NMEA_EXAMPLES_LINK_STATIC "Link examples statically" OFF)
option(NMEA_UNIT_TESTS "Build unit tests" ON)
option(NMEA_UNIT_TESTS_LINK_STATIC "Link unit tests statically" OFF)
option(NMEA_WITH_MEMCHECK "Run unit tests in valgrind" ON)

if (NOT NMEA_BUILD_STATIC_LIB AND NOT NMEA_BUILD_SHARED_LIB)
    message(FATAL_ERROR "You must build either shared or static lib, or both")
endif()

if (NOT NMEA_BUILD_SHARED_LIB)
    set(NMEA_EXAMPLES_LINK_STATIC ON)
    set(NMEA_UNIT_TESTS_LINK_STATIC ON)
endif()

if (NOT NMEA_BUILD_STATIC_LIB)
    message("Linking examples/unit tests to shared lib since NMEA_BUILD_STATIC_LIB is turned off")
    set(NMEA_EXAMPLES_LINK_STATIC OFF)
    set(NMEA_UNIT_TESTS_LINK_STATIC OFF)
endif()

# Set default warning flags for all targets in this directory
add_compile_options(-Wall -Werror)

# Set some nicer output dirs.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)

# This is the location of the parser libraries
set(NMEA_PLUGIN_DIRECTORY ${PROJECT_BINARY_DIR}/parsers/)

set(NMEA_SRC
    src/nmea/nmea.c
    src/nmea/parser.c)

set(NMEA_HDR
    src/nmea/nmea.h
    src/nmea/parser.h
    src/nmea/parser_types.h)

source_group("Headers" FILES ${NMEA_HDR})
source_group("Source" FILES ${NMEA_SRC})

set(LIBNMEA_TARGETS "")
if (NMEA_BUILD_STATIC_LIB)
    set(LIBNMEA_TARGETS ${LIBNMEA_TARGETS} nmea)
    add_library(nmea STATIC
        src/nmea/nmea.c
        src/nmea/parser_static.c
        src/parsers/parse.c)
    set_target_properties(nmea PROPERTIES VERSION ${LIBNMEA_VERSION})
    target_link_libraries(nmea dl)
    install(TARGETS nmea DESTINATION lib)
endif()

if (NMEA_BUILD_SHARED_LIB)
    if (POLICY CMP0042)
        cmake_policy(PUSH)
        cmake_policy(SET CMP0042 OLD)
    endif()

    set(LIBNMEA_TARGETS ${LIBNMEA_TARGETS} nmea_shared)

    add_library(nmea_shared SHARED ${NMEA_SRC})
    set_target_properties(nmea_shared PROPERTIES VERSION ${LIBNMEA_VERSION})
    target_link_libraries(nmea_shared dl)

    if (UNIX)
        set_target_properties(nmea_shared PROPERTIES OUTPUT_NAME nmea)
    endif()

    install(TARGETS nmea_shared DESTINATION lib)
    target_compile_definitions(nmea_shared PRIVATE -DPARSER_PATH="${CMAKE_INSTALL_PREFIX}/lib/nmea/")

    if (POLICY CMP0042)
        cmake_policy(POP)
    endif()
endif()

# Build parsers.
file(GLOB PARSERS_SRCS
    RELATIVE "${PROJECT_SOURCE_DIR}"
    "${PROJECT_SOURCE_DIR}/src/parsers/*.c")
list(REMOVE_ITEM PARSERS_SRCS src/parsers/parse.c)

source_group("Parser Sources" FILES ${PARSERS_SRCS})

foreach(PARSER_SRC ${PARSERS_SRCS})
    get_filename_component(PARSER_NAME ${PARSER_SRC} NAME_WE)
    add_library(${PARSER_NAME} SHARED ${PARSER_SRC} src/parsers/parse.c)

    # Copy parser plugins to separate cmake build directory.
    set_target_properties(${PARSER_NAME} PROPERTIES
         LIBRARY_OUTPUT_DIRECTORY ${NMEA_PLUGIN_DIRECTORY})

    # Install to where we expect this to be on the system (hard coded).
    # Otherwise using absolute paths like this is not the CMake way.
    install(TARGETS ${PARSER_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/nmea/)
endforeach()

# Add parsers to static library
if (NMEA_BUILD_STATIC_LIB)
    list(LENGTH PARSERS_SRCS PARSER_CNT)
    target_compile_definitions(nmea PRIVATE -DPARSER_COUNT=${PARSER_CNT})
    # Add definition for each parser found and add dependency to rename symbols
    foreach(PARSER_SRC ${PARSERS_SRCS})
        get_filename_component(PARSER ${PARSER_SRC} NAME_WE)
        string(TOUPPER ${PARSER} PARSER)
        target_compile_definitions(nmea PRIVATE -DENABLE_${PARSER})
        string(TOLOWER ${PARSER} PARSER)
        add_custom_command(TARGET nmea PRE_LINK
            COMMAND ${CMAKE_OBJCOPY} --redefine-sym init=nmea_${PARSER}_init ${PARSER}.c.o
            COMMAND ${CMAKE_OBJCOPY} --redefine-sym parse=nmea_${PARSER}_parse ${PARSER}.c.o
            COMMAND ${CMAKE_OBJCOPY} --redefine-sym set_default=nmea_${PARSER}_set_default ${PARSER}.c.o
            COMMAND ${CMAKE_OBJCOPY} --redefine-sym allocate_data=nmea_${PARSER}_allocate_data ${PARSER}.c.o
            COMMAND ${CMAKE_OBJCOPY} --redefine-sym free_data=nmea_${PARSER}_free_data ${PARSER}.c.o
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/nmea.dir/src/parsers/)
    endforeach()
    target_sources(nmea PRIVATE ${PARSERS_SRCS})
endif()

# Find parser headers as well.
file(GLOB PARSERS_HDRS
    RELATIVE "${PROJECT_SOURCE_DIR}"
    "${PROJECT_SOURCE_DIR}/src/parsers/*.h")
list(REMOVE_ITEM PARSERS_HDRS parse.h)
list(APPEND ${NMEA_PUB_HDR} ${PARSERS_HDRS})<commit_msg>Update CMake to 3.5<commit_after>cmake_minimum_required (VERSION 3.5)
project(libnmea C)

set(LIBNMEA_MAJOR_VERSION 0)
set(LIBNMEA_MINOR_VERSION 1)
set(LIBNMEA_PATCH_VERSION 2)
set(LIBNMEA_VERSION
  ${LIBNMEA_MAJOR_VERSION}.${LIBNMEA_MINOR_VERSION}.${LIBNMEA_PATCH_VERSION})

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/")

#Define cmake install path
if(WIN32 AND NOT CYGWIN)
  set(DEF_INSTALL_CMAKE_DIR CMake)
else()
  set(DEF_INSTALL_CMAKE_DIR lib/cmake/${PROJECT_NAME})
endif()
set(INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH
  "Installation directory for CMake files")

# Make relative paths absolute (needed later on)
foreach(p LIB BIN INCLUDE CMAKE)
  set(var INSTALL_${p}_DIR)
  if(NOT IS_ABSOLUTE "${${var}}")
    set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
  endif()
endforeach()

option(NMEA_BUILD_STATIC_LIB "Build static libnmea" ON)
option(NMEA_BUILD_SHARED_LIB "Build shared libnmea" ON)
option(NMEA_BUILD_EXAMPLES "Build examples" ON)
option(NMEA_EXAMPLES_LINK_STATIC "Link examples statically" OFF)
option(NMEA_UNIT_TESTS "Build unit tests" ON)
option(NMEA_UNIT_TESTS_LINK_STATIC "Link unit tests statically" OFF)
option(NMEA_WITH_MEMCHECK "Run unit tests in valgrind" ON)

if (NOT NMEA_BUILD_STATIC_LIB AND NOT NMEA_BUILD_SHARED_LIB)
    message(FATAL_ERROR "You must build either shared or static lib, or both")
endif()

if (NOT NMEA_BUILD_SHARED_LIB)
    set(NMEA_EXAMPLES_LINK_STATIC ON)
    set(NMEA_UNIT_TESTS_LINK_STATIC ON)
endif()

if (NOT NMEA_BUILD_STATIC_LIB)
    message("Linking examples/unit tests to shared lib since NMEA_BUILD_STATIC_LIB is turned off")
    set(NMEA_EXAMPLES_LINK_STATIC OFF)
    set(NMEA_UNIT_TESTS_LINK_STATIC OFF)
endif()

# Set default warning flags for all targets in this directory
add_compile_options(-Wall -Werror)

# Set some nicer output dirs.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)

# This is the location of the parser libraries
set(NMEA_PLUGIN_DIRECTORY ${PROJECT_BINARY_DIR}/parsers/)

set(NMEA_SRC
    src/nmea/nmea.c
    src/nmea/parser.c)

set(NMEA_HDR
    src/nmea/nmea.h
    src/nmea/parser.h
    src/nmea/parser_types.h)

source_group("Headers" FILES ${NMEA_HDR})
source_group("Source" FILES ${NMEA_SRC})

set(LIBNMEA_TARGETS "")
if (NMEA_BUILD_STATIC_LIB)
    set(LIBNMEA_TARGETS ${LIBNMEA_TARGETS} nmea)
    add_library(nmea STATIC
        src/nmea/nmea.c
        src/nmea/parser_static.c
        src/parsers/parse.c)
    set_target_properties(nmea PROPERTIES VERSION ${LIBNMEA_VERSION})
    target_include_directories(nmea INTERFACE src/nmea)
    target_link_libraries(nmea dl)
    install(TARGETS nmea DESTINATION lib)
endif()

if (NMEA_BUILD_SHARED_LIB)
    if (POLICY CMP0042)
        cmake_policy(PUSH)
        cmake_policy(SET CMP0042 OLD)
    endif()

    set(LIBNMEA_TARGETS ${LIBNMEA_TARGETS} nmea_shared)

    add_library(nmea_shared SHARED ${NMEA_SRC})
    set_target_properties(nmea_shared PROPERTIES VERSION ${LIBNMEA_VERSION})
    target_include_directories(nmea_shared INTERFACE src/nmea)
    target_link_libraries(nmea_shared dl)

    if (UNIX)
        set_target_properties(nmea_shared PROPERTIES OUTPUT_NAME nmea)
    endif()

    install(TARGETS nmea_shared DESTINATION lib)
    target_compile_definitions(nmea_shared PRIVATE -DPARSER_PATH="${CMAKE_INSTALL_PREFIX}/lib/nmea/")

    if (POLICY CMP0042)
        cmake_policy(POP)
    endif()
endif()

# Build parsers.
file(GLOB PARSERS_SRCS
    RELATIVE "${PROJECT_SOURCE_DIR}"
    "${PROJECT_SOURCE_DIR}/src/parsers/*.c")
list(REMOVE_ITEM PARSERS_SRCS src/parsers/parse.c)

source_group("Parser Sources" FILES ${PARSERS_SRCS})

foreach(PARSER_SRC ${PARSERS_SRCS})
    get_filename_component(PARSER_NAME ${PARSER_SRC} NAME_WE)
    add_library(${PARSER_NAME} SHARED ${PARSER_SRC} src/parsers/parse.c)

    # Copy parser plugins to separate cmake build directory.
    set_target_properties(${PARSER_NAME} PROPERTIES
         LIBRARY_OUTPUT_DIRECTORY ${NMEA_PLUGIN_DIRECTORY})

    # Install to where we expect this to be on the system (hard coded).
    # Otherwise using absolute paths like this is not the CMake way.
    install(TARGETS ${PARSER_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/nmea/)
endforeach()

# Add parsers to static library
if (NMEA_BUILD_STATIC_LIB)
    list(LENGTH PARSERS_SRCS PARSER_CNT)
    target_compile_definitions(nmea PRIVATE -DPARSER_COUNT=${PARSER_CNT})
    # Add definition for each parser found and add dependency to rename symbols
    foreach(PARSER_SRC ${PARSERS_SRCS})
        get_filename_component(PARSER ${PARSER_SRC} NAME_WE)
        string(TOUPPER ${PARSER} PARSER)
        target_compile_definitions(nmea PRIVATE -DENABLE_${PARSER})
        string(TOLOWER ${PARSER} PARSER)
        add_custom_command(TARGET nmea PRE_LINK
            COMMAND ${CMAKE_OBJCOPY} --redefine-sym init=nmea_${PARSER}_init ${PARSER}.c.obj
            COMMAND ${CMAKE_OBJCOPY} --redefine-sym parse=nmea_${PARSER}_parse ${PARSER}.c.obj
            COMMAND ${CMAKE_OBJCOPY} --redefine-sym set_default=nmea_${PARSER}_set_default ${PARSER}.c.obj
            COMMAND ${CMAKE_OBJCOPY} --redefine-sym allocate_data=nmea_${PARSER}_allocate_data ${PARSER}.c.obj
            COMMAND ${CMAKE_OBJCOPY} --redefine-sym free_data=nmea_${PARSER}_free_data ${PARSER}.c.obj
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/nmea.dir/src/parsers/)
    endforeach()
    target_sources(nmea PRIVATE ${PARSERS_SRCS})
endif()

# Find parser headers as well.
file(GLOB PARSERS_HDRS
    RELATIVE "${PROJECT_SOURCE_DIR}"
    "${PROJECT_SOURCE_DIR}/src/parsers/*.h")
list(REMOVE_ITEM PARSERS_HDRS parse.h)
list(APPEND ${NMEA_PUB_HDR} ${PARSERS_HDRS})
{'fraction_non_alphanumeric': 0.098376480912681, 'fraction_numerical': 0.0035103115401491883, 'mean_word_length': 5.123589468027942, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3044758', 'n_tokens_mistral': 4514, 'n_tokens_neox': 4380, 'n_words': 818}