Skip to content

Commit

Permalink
Improve CMake project
Browse files Browse the repository at this point in the history
Added more system checks to build
on different platforms.
  • Loading branch information
podsvirov committed Dec 13, 2017
1 parent ab3b264 commit 978f0b9
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 22 deletions.
69 changes: 68 additions & 1 deletion cmake/CMakeLists.txt
Expand Up @@ -17,6 +17,13 @@ set(${PROJECT_NAME}_MAKE_INSTALL ON CACHE BOOL
set(${PROJECT_NAME}_INSTALL_SAMPLES OFF CACHE BOOL
"Set to ON to install sample programs (default is OFF)")

# Check headers
include(CheckIncludeFile)
check_include_file(libgen.h HAVE_LIBGEN_H)
check_include_file(pwd.h HAVE_PWD_H)
check_include_file(alloca.h HAVE_ALLOCA_H)
check_include_file(sys/stat.h HAVE_STAT)

# Types detection (from configure.inc: AC_SCALAR_TYPES ())
include(CheckTypeSize)
check_type_size("unsigned long" SIZEOF_ULONG BUILTIN_TYPES_ONLY)
Expand All @@ -41,6 +48,66 @@ endif()

set(BYTE "unsigned char")

# Check symbols
include(CheckSymbolExists)
foreach(_symbol
bzero
strcasecmp _stricmp
strncasecmp _strnicmp)
string(TOUPPER ${_symbol} _SYMBOL)
check_symbol_exists(${_symbol} string.h HAVE_${_SYMBOL})
endforeach()
check_symbol_exists(getpwuid pwd.h HAVE_GETPWUID)
check_symbol_exists(basename libgen.h HAVE_BASENAME)
check_symbol_exists(fchdir unistd.h HAVE_FCHDIR)
if(HAVE_STAT)
check_symbol_exists(S_ISCHR sys/stat.h HAVE_S_ISCHR)
check_symbol_exists(S_ISFIFO sys/stat.h HAVE_S_ISFIFO)
check_symbol_exists(S_ISSOCK sys/stat.h HAVE_S_ISSOCK)
endif()

foreach(_SYMBOL
PWD_H GETPWUID
STAT S_ISCHR S_ISFIFO S_ISSOCK
ALLOCA_H
LIBGEN_H BASENAME
FCHDIR
BZERO
STRCASECMP _STRICMP
STRNCASECMP _STRNICMP)
if("${HAVE_${_SYMBOL}}" STREQUAL "")
set(HAVE_${_SYMBOL} 0)
endif()
endforeach()

if(HAVE_LIBGEN_H AND HAVE_BASENAME)
set(DEFINE_HAVE_BASENAME "#define HAVE_BASENAME 1")
endif()

if(NOT HAVE_BZERO)
set(DEFINE_BZERO "#define bzero(p, n) memset(p, 0, n)")
endif()

if(NOT HAVE_STRCASECMP)
if(HAVE__STRICMP)
set(DEFINE_STRCASECMP "#define strcasecmp _stricmp")
else()
set(DEFINE_STRCASECMP "#error The symbol strcasecmp is not defined.")
endif()
endif()

if(NOT HAVE_STRNCASECMP)
if(HAVE__STRNICMP)
set(DEFINE_STRNCASECMP "#define strncasecmp _strnicmp")
else()
set(DEFINE_STRNCASECMP "#error The symbol strncasecmp is not defined.")
endif()
endif()

if(NOT HAVE_S_ISCHR OR NOT HAVE_S_ISFIFO OR NOT HAVE_S_ISSOCK)
set(HAVE_STAT 0)
endif()

configure_file(config.h.in
"${_ROOT}/config.h"
@ONLY)
Expand All @@ -61,7 +128,7 @@ include_directories("${_ROOT}")
add_executable(mktags
"${_ROOT}/mktags.c")

add_custom_command(OUTPUT blocktags
add_custom_command(OUTPUT "${_ROOT}/blocktags"
COMMAND mktags > blocktags
WORKING_DIRECTORY "${_ROOT}")

Expand Down
35 changes: 14 additions & 21 deletions cmake/config.h.in
@@ -1,19 +1,13 @@
/*
* Pre-digested configuration header for Windows.
* Pre-digested configuration header.
* Generated from cmake/config.h.in.
* Tested with MSVC and MinGW.
* Tested with MSVC, MinGW on Windows and with GCC on Linux.
* File prototype: msvc/config.h.vc.
*/

#ifndef _CONFIG_D
#define _CONFIG_D 1

#ifndef WIN32
#error Use this header on Windows only.
#endif

#define OS_WIN32 1

/*
* `discount` feature macros - we want them all!
*/
Expand Down Expand Up @@ -48,13 +42,9 @@

#endif /* _MSC_VER */

#ifndef strncasecmp
#include <string.h>
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#endif /* strncasecmp */

#define bzero(p, n) memset(p, 0, n)
@DEFINE_BZERO@
@DEFINE_STRCASECMP@
@DEFINE_STRNCASECMP@

/*
* Beware of conflicts with <Windows.h>, which typedef's these names.
Expand All @@ -65,17 +55,20 @@
#define BYTE @BYTE@
#endif

#define HAVE_PWD_H 0
#define HAVE_GETPWUID 0
#define HAVE_PWD_H @HAVE_PWD_H@
#define HAVE_GETPWUID @HAVE_GETPWUID@

#define HAVE_LIBGEN_H @HAVE_LIBGEN_H@
@DEFINE_HAVE_BASENAME@

#define HAVE_SRANDOM 0
#define INITRNG(x) srand((unsigned int)x)
#define HAVE_BZERO 0
#define HAVE_RANDOM 0
#define COINTOSS() (rand()&1)
#define HAVE_STRCASECMP 1
#define HAVE_STRNCASECMP 1
#define HAVE_FCHDIR 0
#define HAVE_FCHDIR @HAVE_FCHDIR@
#define TABSTOP @TABSTOP@
#define HAVE_ALLOCA_H @HAVE_ALLOCA_H@
#define HAVE_MALLOC_H 0
#define HAVE_STAT @HAVE_STAT@

#endif /* _CONFIG_D */

0 comments on commit 978f0b9

Please sign in to comment.