initial import

This commit is contained in:
Danny Robson 2017-01-17 19:20:12 +11:00
commit a57db559e9
13 changed files with 525 additions and 0 deletions

112
FindRAGEL.cmake Normal file
View File

@ -0,0 +1,112 @@
# - Find Ragel executable and provides macros to generate custom build rules
# The module defines the following variables:
#
# RAGEL_EXECUTABLE - path to the bison program
# RAGEL_VERSION - version of bison
# RAGEL_FOUND - true if the program was found
#
# If ragel is found, the module defines the macros:
#
# RAGEL_TARGET(<Name> <RagelInp> <CodeOutput>
# [COMPILE_FLAGS <string>])
#
# which will create a custom rule to generate a state machine. <RagelInp> is
# the path to a Ragel file. <CodeOutput> is the name of the source file
# generated by ragel. If COMPILE_FLAGS option is specified, the next
# parameter is added in the bison command line.
#
# The macro defines a set of variables:
# RAGEL_${Name}_DEFINED - true is the macro ran successfully
# RAGEL_${Name}_INPUT - The input source file, an alias for <RagelInp>
# RAGEL_${Name}_OUTPUT_SOURCE - The source file generated by ragel
# RAGEL_${Name}_OUTPUT_HEADER - The header file generated by ragel
# RAGEL_${Name}_OUTPUTS - The sources files generated by ragel
# RAGEL_${Name}_COMPILE_FLAGS - Options used in the ragel command line
#
# ====================================================================
# Example:
#
# find_package(RAGEL) # or e.g.: find_package(RAGEL 6.6 REQUIRED)
# RAGEL_TARGET(MyMachine machine.rl ${CMAKE_CURRENT_BINARY_DIR}/machine.cc)
# add_executable(Foo main.cc ${RAGEL_MyMachine_OUTPUTS})
# ====================================================================
# 2014-02-09, Georg Sauthoff <mail@georg.so>
#
# I don't think that these few lines are even copyrightable material,
# but I am fine with using the BSD/MIT/GPL license on it ...
#
# I've used following references:
# http://www.cmake.org/cmake/help/v2.8.12/cmake.html
# /usr/share/cmake/Modules/FindFLEX.cmake
# /usr/share/cmake/Modules/FindBISON.cmake
# uses some features which are not available in 2.6
cmake_minimum_required(VERSION 2.8)
find_program(RAGEL_EXECUTABLE NAMES ragel DOC "path to the ragel executable")
mark_as_advanced(RAGEL_EXECUTABLE)
if(RAGEL_EXECUTABLE)
execute_process(COMMAND ${RAGEL_EXECUTABLE} --version
OUTPUT_VARIABLE RAGEL_version_output
ERROR_VARIABLE RAGEL_version_error
RESULT_VARIABLE RAGEL_version_result
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(${RAGEL_version_result} EQUAL 0)
string(REGEX REPLACE "^Ragel State Machine Compiler version ([^ ]+) .*$"
"\\1"
RAGEL_VERSION "${RAGEL_version_output}")
else()
message(SEND_ERROR
"Command \"${RAGEL_EXECUTABLE} --version\" failed with output:
${RAGEL_version_error}")
endif()
#============================================================
# RAGEL_TARGET (public macro)
#============================================================
#
macro(RAGEL_TARGET Name Input Output)
set(RAGEL_TARGET_usage
"RAGEL_TARGET(<Name> <Input> <Output> [COMPILE_FLAGS <string>]")
if(${ARGC} GREATER 3)
if(${ARGC} EQUAL 5)
if("${ARGV3}" STREQUAL "COMPILE_FLAGS")
set(RAGEL_EXECUTABLE_opts "${ARGV4}")
separate_arguments(RAGEL_EXECUTABLE_opts)
else()
message(SEND_ERROR ${RAGEL_TARGET_usage})
endif()
else()
message(SEND_ERROR ${RAGEL_TARGET_usage})
endif()
endif()
get_filename_component(Dir "${Output}" DIRECTORY)
file(MAKE_DIRECTORY ${Dir})
add_custom_command(OUTPUT ${Output}
COMMAND ${RAGEL_EXECUTABLE}
ARGS ${RAGEL_EXECUTABLE_opts} -o${Output} ${Input}
DEPENDS ${Input}
COMMENT
"[RAGEL][${Name}] Compiling state machine with Ragel ${RAGEL_VERSION}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
set(RAGEL_${Name}_DEFINED TRUE)
set(RAGEL_${Name}_OUTPUTS ${Output})
set(RAGEL_${Name}_INPUT ${Input})
set(RAGEL_${Name}_COMPILE_FLAGS ${RAGEL_EXECUTABLE_opts})
endmacro()
endif()
# use this include when module file is located under /usr/share/cmake/Modules
#include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
# use this include when module file is located in build tree
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(RAGEL REQUIRED_VARS RAGEL_EXECUTABLE
VERSION_VAR RAGEL_VERSION)

21
canonical_host.cmake Normal file
View File

@ -0,0 +1,21 @@
macro (canonical_host)
if (NOT __test_canonical_host)
message (STATUS "checking the host CPU")
execute_process (
COMMAND ${CMAKE_CXX_COMPILER} -dumpmachine
RESULT_VARIABLE __canonical_host_result
OUTPUT_VARIABLE __canonical_host_output
)
if (${__canonical_host_result})
message (FATAL_ERROR "unable to query for canonical host")
endif (${__canonical_host_result})
string(REGEX MATCH "^([^-\]+)" HOST_CPU ${__canonical_host_output})
message (STATUS "checking the host CPU - found '${HOST_CPU}'")
endif (NOT __test_canonical_host)
set (__test_canonical_host 1 INTERNAL)
endmacro (canonical_host)

11
compile_flag.cmake Normal file
View File

@ -0,0 +1,11 @@
macro (append_compile_flag _flag)
string (MAKE_C_IDENTIFIER ${_flag} _name)
if (NOT __append_compile_flag_${_name})
check_cxx_compiler_flag (${_flag} __append_compile_flag_${_name})
if (__append_compile_flag_${_name})
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_flag}")
set (__append_compile_flag_${_name} 1 INTERNAL)
endif ()
endif ()
endmacro(append_compile_flag)

25
link_flag.cmake Normal file
View File

@ -0,0 +1,25 @@
macro(check_link_flag variable flag)
message (STATUS "checking linker flag ${flag}")
set (__check_linker_flag_old "${CMAKE_EXE_LINKER_FLAGS}" INTERNAL)
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${flag}")
try_compile (test_check_linker_flag
"${CMAKE_BINARY_DIR}/CMakeTmp"
SOURCES "${CMAKE_SOURCE_DIR}/cmake/test_link_flag.cpp")
if (test_check_linker_flag)
message (STATUS "checking linker flag ${flag} - found")
set (${variable} 1)
else(test_check_linker_flag)
message (STATUS "checking linker flag ${flag} - not found")
set (${variable} 0)
endif(test_check_linker_flag)
endmacro()
macro(append_link_flag flag)
check_link_flag (__test_link_flag_${flag} ${flag})
if (__test_link_flag_${flag})
set (CMAKE_EXE_LINKER_FLAGS "${__append_link_flag_old}")
endif()
endmacro()

25
nc_cxx.cmake Normal file
View File

@ -0,0 +1,25 @@
include (compile_flag)
include (test_restrict)
###############################################################################
append_compile_flag (-std=c++1z)
###############################################################################
# find the gcc experimental filesystem library and append to libs if needed
find_library(STDCXXFS NAMES stdc++fs)
if (STDCXXFS)
set(LIBS ${LIBS} ${STDCXXFS})
endif (STDCXXFS)
###############################################################################
test_restrict(RESTRICT_KEYWORD)
add_definitions("-Drestrict=${RESTRICT_KEYWORD}")
###############################################################################
append_compile_flag("-pipe")
append_compile_flag("-fno-deduce-init-list")
append_compile_flag("-fvisibility=hidden")

121
nc_optimisation.cmake Normal file
View File

@ -0,0 +1,121 @@
include (compile_flag)
include (link_flag)
include (canonical_host)
###############################################################################
option(ENABLE_LTO "enable link-time optimisation" OFF)
##-----------------------------------------------------------------------------
if (ENABLE_LTO)
append_compile_flag("-flto")
append_compile_flag("-fno-fat-lto-objects")
append_link_flag("-fuse-linker-plugin")
append_compile_flags("-fdevirtualize-at-ltrans")
endif (ENABLE_LTO)
###############################################################################
option(ENABLE_FRAMEPOINTER "retain the framepointer even if optimising" OFF)
##-----------------------------------------------------------------------------
if (ENABLE_FRAMEPOINTER)
append_compile_flags("-fno-omit-frame-pointer")
endif (ENABLE_FRAMEPOINTER)
###############################################################################
canonical_host()
##-----------------------------------------------------------------------------
if (${HOST_CPU} STREQUAL "x86_64")
append_compile_flag(-mtune=generic)
append_compile_flag(-msse)
append_compile_flag(-msse2)
append_compile_flag(-msahf)
elseif (${HOST_CPU} STREQUAL "i686")
append_compile_flag(-march=prescott)
append_compile_flag(-mtune=generic)
append_compile_flag(-mcmov)
append_compile_flag(-mfpmath=sse)
else ()
message (FATAL_ERROR "unknown HOST_CPU '${HOST_CPU}'")
endif ()
###############################################################################
if (CMAKE_BUILD_TYPE MATCHES DEBUG)
else()
append_compile_flag(-ftree-vectorize)
# clang vectorisation
append_compile_flag(-fvectorize)
append_compile_flag(-fslp-vectorize)
append_compile_flag(-fslp-vectorize-aggressive)
# loop hosting/distribution
append_compile_flag(-floop-nest-optimize)
append_compile_flag(-ftree-loop-distribution)
append_compile_flag(-ftree-loop-distribute-patterns)
append_compile_flag(-ftree-loop-im)
append_compile_flag(-ftree-loop-if-convert-stores)
append_compile_flag(-fivopts)
append_compile_flag(-funsafe-loop-optimizations)
append_compile_flag(-floop-interchange)
append_compile_flag(-fpredictive-commoning)
append_compile_flag(-funswitch-loops)
# options that require substantial time/memory
# GCC: ipa-pta can produce broken code when using LTO
#AS_IF([test "x$ax_cv_cxx_compiler_vendor" != "xgnu" ||
# test "x$enable_lto" != "xyes"], [
# AX_APPEND_COMPILE_FLAGS([-fipa-pta])
#])
# safety removal for performance
append_compile_flag(-fno-stack-protector)
endif()
###############################################################################
if (CMAKE_BUILD_TYPE MATCHES DEBUG)
else ()
append_compile_flag(-fdevirtualize)
append_compile_flag(-fdevirtualize-speculatively)
check_link_flag(TEST_GC_SECTIONS, "-Wl,--gc-sections")
if (TEST_LD_GC_SECTIONS)
append_compile_flag(-fdata-sections)
append_compile_flag(-ffunction-sections)
append_link_flag("-Wl,--gc-sections")
endif ()
endif ()
###############################################################################
if (CMAKE_BUILD_TYPE MATCHES DEBUG)
add_definitions(-DENABLE_DEBUGGING)
add_definitions(-D_GLIBCXX_DEBUG)
append_compile_flag(-O0)
append_compile_flag(-Werror)
# this was protected for xmingw32, find out why
append_compile_flag(-fstack-protector)
else ()
append_compile_flag(-O2)
append_compile_flag(-fno-rtti)
add_definitions(-DNO_RTTI)
add_definitions(-DNDEBUG)
endif ()
###############################################################################
append_compile_flag(-g)
append_compile_flag(-ggdb)

31
nc_platform.cmake Normal file
View File

@ -0,0 +1,31 @@
include (compile_flag)
include (link_flag)
###############################################################################
# attempt to detect platforms that aren't super common
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
set(LINUX 1)
elseif (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
set(FREEBSD 1)
else()
message (FATAL_ERROR "unknown platform")
endif()
###############################################################################
# apply useful, non-obvious, default settings for each platform
if (WINDOWS)
add_definitions("-DWIN32_LEAN_AND_MEAN")
endif(WINDOWS)
if (FREEBSD)
append_compile_flag("-I/usr/local/include")
append_link_flag("-L/usr/local/lib")
endif (FREEBSD)
###############################################################################
# general platform assistance settings
append_link_flag("-fno-common")
append_link_flag("-fno-nonansi-builtins")

87
nc_warnings.cmake Normal file
View File

@ -0,0 +1,87 @@
include (compile_flag)
append_compile_flag(-Wall)
append_compile_flag(-Wextra)
# -------------------------------------------------------------------------
# General warnings
append_compile_flag(-Wbool-compare)
append_compile_flag(-Wcast-align)
append_compile_flag(-Wcast-qual)
append_compile_flag(-Wdisabled-optimization)
append_compile_flag(-Wfloat-conversion)
append_compile_flag(-Wfloat-equal)
append_compile_flag(-Wno-aggressive-loop-optimizations)
append_compile_flag(-Wnoexcept)
append_compile_flag(-Wnon-virtual-dtor)
append_compile_flag(-Wno-parentheses)
append_compile_flag(-Wpointer-arith)
append_compile_flag(-Wredundant-decls)
append_compile_flag(-Wshadow)
append_compile_flag(-Wsign-compare)
append_compile_flag(-Wsign-promo)
append_compile_flag(-Wstrict-aliasing)
append_compile_flag(-Wstrict-overflow)
append_compile_flag(-Wtype-limits)
append_compile_flag(-Wunsafe-loop-optimizations)
append_compile_flag(-Wunused-but-set-variable)
append_compile_flag(-Wunused-parameter)
append_compile_flag(-Wpessimizing-move)
append_compile_flag(-Wswitch-enum)
# gcc-6.1.0 warnings
append_compile_flag(-Wshift-negative-value)
append_compile_flag(-Wnull-dereference)
append_compile_flag(-Wduplicated-cond)
# useless-cast isn't usable on gcc-6.1 due to spurious warnings
# see gcc#70844
#AS_IF([test "x$ax_cv_cxx_compiler_vendor" != "xgnu" || test "x$ax_cv_cxx_compiler_version" != "x6.1.0"], [
append_compile_flag(-Wuseless-cast)
#])
# clang 3.7.1 warnings
append_compile_flag(-Wshorten-64-to-32)
append_compile_flag(-Wdeprecated)
append_compile_flag(-Wcovered-switch-default)
# -------------------------------------------------------------------------
# Required extensions
#AX_APPEND_COMPILE_FLAGS([-Wgnu-flexible-array-union-member], [], [-Werror])
#AX_APPEND_COMPILE_FLAGS([-Wno-c99-extensions], [], [-Werror])
#AX_APPEND_COMPILE_FLAGS([-Wno-vla-extension], [], [-Werror])
append_compile_flag(-Wno-vla)
append_compile_flag(-Wno-multichar)
#AS_IF([test "x$ax_cv_cxx_compiler_vendor" != "xgnu"], [
# AX_APPEND_COMPILE_FLAGS([-Wno-c99-extensions], [], [-Werror])
# AX_APPEND_COMPILE_FLAGS([-Wno-flexible-array-extensions], [], [-Werror])
# AX_APPEND_COMPILE_FLAGS([-Wno-gnu-conditional-omitted-operand], [], [-Werror])
# AX_APPEND_COMPILE_FLAGS([-Wno-gnu-empty-struct], [], [-Werror])
# AX_APPEND_COMPILE_FLAGS([-Wno-gnu-flexible-array-union-member], [], [-Werror])
# AX_APPEND_COMPILE_FLAGS([-Wno-gnu-zero-variadic-macro-arguments], [], [-Werror])
# AX_APPEND_COMPILE_FLAGS([-Wno-vla-extension], [], [-Werror])
# AX_APPEND_COMPILE_FLAGS([-Wno-zero-length-array], [], [-Werror])
#])
# -------------------------------------------------------------------------
# Excessive warnings
append_compile_flag(-Wno-missing-braces)
#AS_IF([test "x$ax_cv_cxx_compiler_vendor" != "xgnu"], [
# AX_APPEND_COMPILE_FLAGS([-Wno-nested-anon-types], [], [-Werror])
# AX_APPEND_COMPILE_FLAGS([-Wno-unused-const-variable], [], [-Werror])
#])
# -------------------------------------------------------------------------
option (ADVISORY "enable advisory warnings")
if (ADVISORY)
append_compile_flag(-Winline)
append_compile_flag(-Wsuggest-final-types)
append_compile_flag(-Wsuggest-final-methods)
append_compile_flag(-Wsuggest-override)
append_compile_flag(-Wweak-vtables)
append_compile_flag(-Wpadded)
endif()

39
search_libs.cmake Normal file
View File

@ -0,0 +1,39 @@
# search a list of libraries for a given (C linkage) _symbol. returns the
# required library in _variable, and defines HAVE_SYMBOL if it was found.
macro(search_libs _variable _symbol)
message (STATUS "searching for ${_symbol}")
try_compile(
__search_libs_${_symbol}
"${CMAKE_BINARY_DIR}/CMakeTmp"
SOURCES "${CMAKE_SOURCE_DIR}/cmake/search_libs.cpp"
COMPILE_DEFINITIONS "-DSYMBOL=${_symbol}")
if (NOT __search_libs_${_symbol})
foreach (lib ${ARGN})
if (NOT __search_libs_${_symbol}_last)
message (STATUS "searching for ${_symbol} in ${lib}")
try_compile(
__search_libs_${_symbol}
"${CMAKE_BINARY_DIR}/CMakeTmp"
SOURCES "${CMAKE_SOURCE_DIR}/cmake/search_libs.cpp"
LINK_LIBRARIES "-l${lib}"
COMPILE_DEFINITIONS "-DSYMBOL=${_symbol}")
set (__search_libs_${_symbol}_last ${lib})
endif ()
endforeach()
endif()
if (__search_libs_${_symbol})
if (__search_libs_${_symbol}_last)
message (STATUS "searching for ${_symbol} - found (in ${__search_libs_${_symbol}_last})")
else ()
message (STATUS "searching for ${_symbol} - found")
endif ()
set (${_variable} __search_libs_${_symbol}_last)
string (TOUPPER ${_symbol} upper_symbol)
set (HAVE_${upper_symbol} 1)
else ()
message (STATUS "searching for ${_symbol} - not found")
endif ()
endmacro()

13
search_libs.cpp Normal file
View File

@ -0,0 +1,13 @@
extern "C" {
extern void SYMBOL (void);
}
int
main (int argc, char **argv)
{
(void)argc;
(void)argv;
SYMBOL ();
}

1
test_link_flag.cpp Normal file
View File

@ -0,0 +1 @@
int main (int argc, char **argv) { (void)argc; (void)argv; return 0; }

24
test_restrict.cmake Normal file
View File

@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.7.1)
macro(test_restrict VARIABLE)
if (NOT DEFINED TEST_${VARIABLE})
message (STATUS "checking for restrict keyword")
FOREACH(KEYWORD "restrict" "__restrict" "__restrict__" "_Restrict")
IF (NOT TEST_${VARIABLE})
TRY_COMPILE(
TEST_${VARIABLE}
"${CMAKE_BINARY_DIR}/CMakeTmp"
SOURCES "${CMAKE_SOURCE_DIR}/cmake/test_restrict.cpp"
COMPILE_DEFINITIONS "-DKEYWORD=${KEYWORD}")
set(__test_restrict_last ${KEYWORD})
ENDIF (NOT TEST_${VARIABLE})
endforeach(KEYWORD)
if (TEST_${VARIABLE})
message (STATUS "checking for restrict keyword - ${__test_restrict_last}")
set(${VARIABLE} ${__test_restrict_last} CACHE INTERNAL "restrict keyword" FORCE)
else (TEST_${VARIABLE})
message (STATUS "checking for restrict keyword - not found")
endif(TEST_${VARIABLE})
endif(NOT DEFINED TEST_${VARIABLE})
endmacro(test_restrict)

15
test_restrict.cpp Normal file
View File

@ -0,0 +1,15 @@
typedef int * int_ptr;
int foo (int_ptr KEYWORD ip) {
return ip[0];
}
int
main (int argc, char **argv){
(void)argc;
(void)argv;
int s[1];
int * KEYWORD t = s;
t[0] = 0;
return foo(t);
}