Skip to content

Commit

Permalink
Build system cleanup.
Browse files Browse the repository at this point in the history
Uncrustify target now works.
Propper versioning.
  • Loading branch information
palerikm committed Feb 8, 2016
1 parent 812cb5b commit 3f4f860
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 20 deletions.
33 changes: 15 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
# top level build file for the sockaddrutil library

## prepare CMAKE
cmake_minimum_required ( VERSION 3.0.0 )
cmake_minimum_required ( VERSION 3.2.0 )
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
include ( Uncrustify )
include ( GetGitRevisionDescription )
git_describe(VERSION --tags --dirty=-d)

set ( SOCKADDRUTIL_VERSION_MAJOR 0 CACHE STRING "Project major version number")
set ( SOCKADDRUTIL_VERSION_MINOR "3" CACHE STRING "Project minor version number" )
set ( SOCKADDRUTIL_VERSION_PATCH "0" CACHE STRING "Project patch version number" )
mark_as_advanced(SOCKADDRUTIL_VERSION_MAJOR SOCKADDRUTIL_VERSION_MINOR SOCKADDRUTIL_VERSION_PATCH)
string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" SOCKADDRUTIL_VERSION_MAJOR "${VERSION}")
string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" SOCKADDRUTIL_VERSION_MINOR "${VERSION}")
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" SOCKADDRUTIL_VERSION_PATCH "${VERSION}")
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+(.*)" "\\1" SOCKADDRUTIL_VERSION_SHA1 "${VERSION}")

mark_as_advanced(SOCKADDRUTIL_VERSION_MAJOR SOCKADDRUTIL_VERSION_MINOR SOCADDRUTIL_VERSION_PATCH)

set ( SOCKADDRUTIL_VERSION "${SOCKADDRUTIL_VERSION_MAJOR}.${SOCKADDRUTIL_VERSION_MINOR}.${SOCKADDRUTIL_VERSION_PATCH}" )

Expand All @@ -23,6 +29,7 @@ option ( fatal_warnings "Treat build warnings as errors" ON )
option ( coveralls "Generate coveralls data" ON )
option ( coveralls_send "Send data to coveralls site" OFF )
option ( build_docs "Create docs using Doxygen" ${DOXYGEN_FOUND} )
option ( uncrustify "Uncrustify the source code" ${UNCRUSTIFY_FOUND} )

set ( dist_dir ${CMAKE_BINARY_DIR}/dist )
set ( prefix ${CMAKE_INSTALL_PREFIX} )
Expand Down Expand Up @@ -50,17 +57,7 @@ include ( CheckTypeSize )
check_include_files ( stdint.h HAVE_STDINT_H )
check_include_files ( stdlib.h HAVE_STDLIB_H )
check_include_files ( stdbool.h HAVE_STDBOOL_H )
#check_function_exists ( arc4random HAVE_ARC4RANDOM )
#check_library_exists ( pthread pthread_create "" HAVE_LIBPTHREAD )
#check_library_exists ( m tan "" HAVE_LIBM )

## check for urandom
#if ( EXISTS "/dev/urandom" )
# set ( HAVE__DEV_URANDOM 1 CACHE INTERNAL "/dev/urandom exists" )
# message ( STATUS "Looking for /dev/urandom - found" )
#elseif ()
# message ( STATUS "Looking for /dev/urandom - NOT FOUND" )
#endif ()


configure_file ( ${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h )

Expand Down Expand Up @@ -125,10 +122,10 @@ set ( CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE" )

include ( CPack )
include ( CTest )

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake)
include ( LCov )

UncrustifyTop(${uncrustify})

if (build_docs)
if(NOT DOXYGEN_FOUND)
message(FATAL_ERROR "Doxygen is needed to build the documentation.")
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![Build Status](https://travis-ci.org/NATTools/sockaddrutil.svg?branch=master)](https://travis-ci.org/NATTools/sockaddrutil)
[![Coverity Scan Build Status](https://scan.coverity.com/projects/7586/badge.svg)](https://scan.coverity.com/projects/nattools-sockaddrutil)
[![Coverage Status](https://coveralls.io/repos/NATTools/sockaddrutil/badge.svg?branch=master&service=github)](https://coveralls.io/github/NATTools/sockaddrutil?branch=master)
[![GitHub version](https://badge.fury.io/gh/NATTools%2Fsockaddrutil.svg)](https://github.com/NATTools/sockaddrutil/releases)

# Sockaddrutil
Idea is to make it a little less painfull to work with sockaddr and friends.
Expand All @@ -19,14 +20,14 @@ against in build/dist/lib.

## Development

You need to have [cmake](http://www.cmake.org/ to build.
You need to have [cmake](http://www.cmake.org/) to build.
Note that version 3.2 or newer is needed. (Some linux distributions are a bit slow to update, so manuall install may be needed.)

### Unit Tests

Build and run the checks with

make test
make -C build test

If tests fail it can help to run the binaries in `build/dist/test` to see where
they fail.
Expand All @@ -47,6 +48,9 @@ friends, but do not give up! (OS-X, clang and friends are tricky)

Using uncrustify to clean up code. There is a uncrustify.cfg file that describes
the format. Run uncrustify before posting pull requests.

[Semantic versioning](http://semver.org/) is used.

## Contributing

Fork and send pull requests. Please add your name to the AUTHORS list in your
Expand Down
130 changes: 130 additions & 0 deletions cmake/GetGitRevisionDescription.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# - Returns a version string from Git
#
# These functions force a re-configure on each git commit so that you can
# trust the values of the variables in your build system.
#
# get_git_head_revision(<refspecvar> <hashvar> [<additional arguments to git describe> ...])
#
# Returns the refspec and sha hash of the current head revision
#
# git_describe(<var> [<additional arguments to git describe> ...])
#
# Returns the results of git describe on the source tree, and adjusting
# the output so that it tests false if an error occurs.
#
# git_get_exact_tag(<var> [<additional arguments to git describe> ...])
#
# Returns the results of git describe --exact-match on the source tree,
# and adjusting the output so that it tests false if there was no exact
# matching tag.
#
# Requires CMake 2.6 or newer (uses the 'function' command)
#
# Original Author:
# 2009-2010 Ryan Pavlik <[email protected]> <[email protected]>
# http://academic.cleardefinition.com
# Iowa State University HCI Graduate Program/VRAC
#
# Copyright Iowa State University 2009-2010.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

if(__get_git_revision_description)
return()
endif()
set(__get_git_revision_description YES)

# We must run the following at "include" time, not at function call time,
# to find the path to this module rather than the path to a calling list file
get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)

function(get_git_head_revision _refspecvar _hashvar)
set(GIT_PARENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(GIT_DIR "${GIT_PARENT_DIR}/.git")
while(NOT EXISTS "${GIT_DIR}") # .git dir not found, search parent directories
set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}")
get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
# We have reached the root directory, we are not in git
set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
return()
endif()
set(GIT_DIR "${GIT_PARENT_DIR}/.git")
endwhile()
# check if this is a submodule
if(NOT IS_DIRECTORY ${GIT_DIR})
file(READ ${GIT_DIR} submodule)
string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule})
get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
endif()
set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
if(NOT EXISTS "${GIT_DATA}")
file(MAKE_DIRECTORY "${GIT_DATA}")
endif()

if(NOT EXISTS "${GIT_DIR}/HEAD")
return()
endif()
set(HEAD_FILE "${GIT_DATA}/HEAD")
configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)

configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
"${GIT_DATA}/grabRef.cmake"
@ONLY)
include("${GIT_DATA}/grabRef.cmake")

set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
endfunction()

function(git_describe _var)
if(NOT GIT_FOUND)
find_package(Git QUIET)
endif()
get_git_head_revision(refspec hash)
if(NOT GIT_FOUND)
set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
return()
endif()
if(NOT hash)
set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
return()
endif()

# TODO sanitize
#if((${ARGN}" MATCHES "&&") OR
# (ARGN MATCHES "||") OR
# (ARGN MATCHES "\\;"))
# message("Please report the following error to the project!")
# message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
#endif()

#message(STATUS "Arguments to execute_process: ${ARGN}")

execute_process(COMMAND
"${GIT_EXECUTABLE}"
describe
# ${hash}
${ARGN}
WORKING_DIRECTORY
"${CMAKE_CURRENT_SOURCE_DIR}"
RESULT_VARIABLE
res
OUTPUT_VARIABLE
out
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT res EQUAL 0)
set(out "${out}-${res}-NOTFOUND")
endif()

set(${_var} "${out}" PARENT_SCOPE)
endfunction()

function(git_get_exact_tag _var)
git_describe(out --exact-match ${ARGN})
set(${_var} "${out}" PARENT_SCOPE)
endfunction()
41 changes: 41 additions & 0 deletions cmake/GetGitRevisionDescription.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# Internal file for GetGitRevisionDescription.cmake
#
# Requires CMake 2.6 or newer (uses the 'function' command)
#
# Original Author:
# 2009-2010 Ryan Pavlik <[email protected]> <[email protected]>
# http://academic.cleardefinition.com
# Iowa State University HCI Graduate Program/VRAC
#
# Copyright Iowa State University 2009-2010.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

set(HEAD_HASH)

file(READ "@HEAD_FILE@" HEAD_CONTENTS LIMIT 1024)

string(STRIP "${HEAD_CONTENTS}" HEAD_CONTENTS)
if(HEAD_CONTENTS MATCHES "ref")
# named branch
string(REPLACE "ref: " "" HEAD_REF "${HEAD_CONTENTS}")
if(EXISTS "@GIT_DIR@/${HEAD_REF}")
configure_file("@GIT_DIR@/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY)
else()
configure_file("@GIT_DIR@/packed-refs" "@GIT_DATA@/packed-refs" COPYONLY)
file(READ "@GIT_DATA@/packed-refs" PACKED_REFS)
if(${PACKED_REFS} MATCHES "([0-9a-z]*) ${HEAD_REF}")
set(HEAD_HASH "${CMAKE_MATCH_1}")
endif()
endif()
else()
# detached HEAD
configure_file("@GIT_DIR@/HEAD" "@GIT_DATA@/head-ref" COPYONLY)
endif()

if(NOT HEAD_HASH)
file(READ "@GIT_DATA@/head-ref" HEAD_HASH LIMIT 1024)
string(STRIP "${HEAD_HASH}" HEAD_HASH)
endif()
37 changes: 37 additions & 0 deletions cmake/Uncrustify.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
find_program(UNCRUSTIFY_EXE uncrustify)
if (DEFINED UNCRUSTIFY_EXE)
set (UNCRUSTIFY_FOUND ON)
set (uncrustify_cfg "${PROJECT_SOURCE_DIR}/uncrustify.cfg")
else()
set (UNCRUSTIFY_FOUND OFF)
endif()

function(UncrustifyTop enabled)
if (NOT enabled)
return()
endif()

if (NOT UNCRUSTIFY_FOUND)
message(FATAL_ERROR "Uncrustify is needed to pretty up the source.")
endif()

add_custom_target(uncrustify
COMMENT "Prettying source code with uncrustify")
endfunction()

function(UncrustifyDir file_list_var)
get_filename_component(THIS_DIR "${CMAKE_CURRENT_SOURCE_DIR}" NAME)
set(THIS_TS "${CMAKE_CURRENT_SOURCE_DIR}/.uncrustify_time")

add_custom_command(
OUTPUT "${THIS_TS}"
COMMAND "${UNCRUSTIFY_EXE}" --replace --no-backup -c "${uncrustify_cfg}" ${${file_list_var}}
COMMAND touch "${THIS_TS}"
DEPENDS ${${file_list_var}} "${uncrustify_cfg}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
COMMENT "Uncrustifying ${THIS_DIR}")
add_custom_target(
"uncrustify-${THIS_DIR}"
DEPENDS "${THIS_TS}")
add_dependencies(uncrustify "uncrustify-${THIS_DIR}")
endfunction()

0 comments on commit 3f4f860

Please sign in to comment.