From 8afb73c00c34319c6b3c3317413a3ac0d032336b Mon Sep 17 00:00:00 2001 From: Oscar Broman Date: Fri, 10 Jan 2014 15:34:53 +0100 Subject: [PATCH 1/7] make it compile on OS X --- source/amx/osdefs.h | 2 ++ source/compiler/CMakeLists.txt | 12 +++++++++++- source/compiler/memfile.h | 2 +- source/compiler/sc1.c | 17 +++++++++++------ source/compiler/sc6.c | 2 +- source/linux/sclinux.h | 6 ++++++ 6 files changed, 32 insertions(+), 9 deletions(-) diff --git a/source/amx/osdefs.h b/source/amx/osdefs.h index 29804ab..ae9162a 100644 --- a/source/amx/osdefs.h +++ b/source/amx/osdefs.h @@ -37,6 +37,8 @@ #if defined __FreeBSD__ #include +#elif defined __APPLE__ + #include #elif defined LINUX #include #endif diff --git a/source/compiler/CMakeLists.txt b/source/compiler/CMakeLists.txt index 6f0feda..005ff4a 100644 --- a/source/compiler/CMakeLists.txt +++ b/source/compiler/CMakeLists.txt @@ -43,6 +43,15 @@ endif() configure_file(svnrev.h.in svnrev.h) include_directories(${CMAKE_CURRENT_BINARY_DIR}) +IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_FORTIFY_SOURCE=0") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-logical-op-parentheses") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-ignored-attributes") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-incompatible-pointer-types-discards-qualifiers") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-format") +ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + # The Pawn compiler shared library set(PAWNC_SRCS sc1.c sc2.c sc3.c sc4.c sc5.c sc6.c sc7.c scexpand.c sci18n.c sclist.c scmemfil.c scstate.c scvars.c @@ -66,7 +75,8 @@ add_library(pawnc SHARED ${PAWNC_SRCS}) if(WATCOM) #Watcom C/C++ does not support a .DEF file for the exports set_target_properties(pawnc PROPERTIES LINK_FLAGS "/exp=libpawnc") endif() -if(UNIX AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug") +if(UNIX AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug" + AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin") # Unix exports all symbols by default; we want only a very select few exported symbols add_custom_command(TARGET pawnc POST_BUILD COMMAND strip ARGS -K pc_compile -K pc_addconstant -K pc_addtag -K pc_enablewarning ${CMAKE_BINARY_DIR}/libpawnc.so) endif() diff --git a/source/compiler/memfile.h b/source/compiler/memfile.h index 5b4930f..8cef0d0 100644 --- a/source/compiler/memfile.h +++ b/source/compiler/memfile.h @@ -8,7 +8,7 @@ #ifndef _INCLUDE_MEMFILE_H #define _INCLUDE_MEMFILE_H -#ifdef MACOS +#ifdef __APPLE__ #include #else #include diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 53d4483..41f49e2 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -40,6 +40,10 @@ #include /* from BinReloc, see www.autopackage.org */ #endif +#if defined __APPLE__ + #include +#endif + #if defined FORTIFY #include #endif @@ -472,19 +476,20 @@ int pc_compile(int argc, char *argv[]) char *tname,*sname; FILE *ftmp,*fsrc; int fidx; + ftmp = NULL; #if defined __WIN32__ || defined _WIN32 tname=_tempnam(NULL,"pawn"); #elif defined __MSDOS__ || defined _Windows tname=tempnam(NULL,"pawn"); - #elif defined(MACOS) && !defined(__MACH__) - /* tempnam is not supported for the Macintosh CFM build. */ - error(104,get_sourcefile(1)); - tname=NULL; - sname=NULL; + #elif defined(__APPLE__) + int tfd; + tfd=mkstemp("/tmp/pawnXXXXXX"); + ftmp=fdopen(tfd, "wt"); #else tname=tempnam(NULL,"pawn"); #endif - ftmp=(FILE*)pc_createsrc(tname); + if (ftmp==NULL) + ftmp=(FILE*)pc_createsrc(tname); for (fidx=0; (sname=get_sourcefile(fidx))!=NULL; fidx++) { unsigned char tstring[128]; fsrc=(FILE*)pc_opensrc(sname); diff --git a/source/compiler/sc6.c b/source/compiler/sc6.c index df6b61a..04c38f8 100644 --- a/source/compiler/sc6.c +++ b/source/compiler/sc6.c @@ -215,7 +215,7 @@ static char *stripwhitespace(char *str) { if (*str!='\0') { size_t len = strlen(str); - size_t i; + int i; for (i=len-1; i>=0; i--) { if (!isspace(str[i])) { str[i+1]='\0'; diff --git a/source/linux/sclinux.h b/source/linux/sclinux.h index f22497a..3b4af2c 100644 --- a/source/linux/sclinux.h +++ b/source/linux/sclinux.h @@ -40,6 +40,12 @@ # define __BIG_ENDIAN BIG_ENDIAN #endif +#if defined __APPLE__ +# define __BYTE_ORDER BYTE_ORDER +# define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN +# define __BIG_ENDIAN __DARWIN_BIG_ENDIAN +#endif + #if !defined __BYTE_ORDER # error "Can't figure computer byte order (__BYTE_ORDER macro not found)" #endif From f9fde04a1a3cd78388d00c93caf53b18e9f8524a Mon Sep 17 00:00:00 2001 From: Zeex Date: Mon, 20 Jan 2014 22:55:28 +0700 Subject: [PATCH 2/7] Don't suppress compile warnings --- source/compiler/CMakeLists.txt | 9 --------- 1 file changed, 9 deletions(-) diff --git a/source/compiler/CMakeLists.txt b/source/compiler/CMakeLists.txt index 005ff4a..6531c9c 100644 --- a/source/compiler/CMakeLists.txt +++ b/source/compiler/CMakeLists.txt @@ -43,15 +43,6 @@ endif() configure_file(svnrev.h.in svnrev.h) include_directories(${CMAKE_CURRENT_BINARY_DIR}) -IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_FORTIFY_SOURCE=0") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-logical-op-parentheses") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-ignored-attributes") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-incompatible-pointer-types-discards-qualifiers") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-format") -ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") - # The Pawn compiler shared library set(PAWNC_SRCS sc1.c sc2.c sc3.c sc4.c sc5.c sc6.c sc7.c scexpand.c sci18n.c sclist.c scmemfil.c scstate.c scvars.c From d6b77d645a53e92a2ef52e6e35a2353a9a818c8b Mon Sep 17 00:00:00 2001 From: Zeex Date: Mon, 20 Jan 2014 23:04:00 +0700 Subject: [PATCH 3/7] Revert unrelated change --- source/compiler/sc6.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/compiler/sc6.c b/source/compiler/sc6.c index 04c38f8..df6b61a 100644 --- a/source/compiler/sc6.c +++ b/source/compiler/sc6.c @@ -215,7 +215,7 @@ static char *stripwhitespace(char *str) { if (*str!='\0') { size_t len = strlen(str); - int i; + size_t i; for (i=len-1; i>=0; i--) { if (!isspace(str[i])) { str[i+1]='\0'; From 12afdf16c2cf6080c8165f40830076fd8f1d1f73 Mon Sep 17 00:00:00 2001 From: Zeex Date: Sat, 4 Apr 2015 17:41:31 +0600 Subject: [PATCH 4/7] Refactor temporary source file creation This adds a new function pc_createtmpsrc() that creates a temporary source file for writing. Also, mkstemp() is now used not only on Mac OS X but on Linux (and other Unixes) as well. --- source/compiler/libpawnc.c | 37 +++++++++++++++++++++++++++ source/compiler/sc1.c | 52 ++++++++++++++++++++++++++++---------- 2 files changed, 75 insertions(+), 14 deletions(-) diff --git a/source/compiler/libpawnc.c b/source/compiler/libpawnc.c index 025ef01..c12031a 100644 --- a/source/compiler/libpawnc.c +++ b/source/compiler/libpawnc.c @@ -178,6 +178,43 @@ void *pc_createsrc(char *filename) return fopen(filename,"wt"); } +/* pc_createtmpsrc() + * Creates a temporary source file with a unique name for writing. + * Return: + * The function must return a pointer, which is used as a "magic cookie" to + * all I/O functions. When failing to open the file for reading, the + * function must return NULL. + */ +void *pc_createtmpsrc(char **filename) +{ + char *tname=NULL; + FILE *ftmp=NULL; + + #if defined __WIN32__ || defined _WIN32 + tname=_tempnam(NULL,"pawn"); + ftmp=fopen(tname,"wt"); + #elif defined __MSDOS__ || defined _Windows + tname=tempnam(NULL,"pawn"); + ftmp=fopen(tname,"wt"); + #else + static const char template[]="/tmp/pawnXXXXXX"; + ftmp=NULL; + if ((tname=malloc(sizeof(template)))!=NULL) { + int fdtmp; + strncpy(tname,template,sizeof(template)); + if ((fdtmp=mkstemp(tname)) >= 0) { + ftmp=fdopen(fdtmp,"wt"); + } else { + free(tname); + tname=NULL; + } /* if */ + } /* if */ + #endif + if (filename!=NULL) + *filename=tname; + return ftmp; +} + /* pc_closesrc() * Closes a source file (or include file). The "handle" parameter has the * value that pc_opensrc() returned in an earlier call. diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 41f49e2..ee3959f 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -254,6 +254,43 @@ void *pc_createsrc(char *filename) return fopen(filename,"w"); } +/* pc_createtmpsrc() + * Creates a temporary source file with a unique name for writing. + * Return: + * The function must return a pointer, which is used as a "magic cookie" to + * all I/O functions. When failing to open the file for reading, the + * function must return NULL. + */ +void *pc_createtmpsrc(char **filename) +{ + char *tname=NULL; + FILE *ftmp=NULL; + + #if defined __WIN32__ || defined _WIN32 + tname=_tempnam(NULL,"pawn"); + ftmp=fopen(tname,"wt"); + #elif defined __MSDOS__ || defined _Windows + tname=tempnam(NULL,"pawn"); + ftmp=fopen(tname,"wt"); + #else + static const char template[]="/tmp/pawnXXXXXX"; + ftmp=NULL; + if ((tname=malloc(sizeof(template)))!=NULL) { + int fdtmp; + strncpy(tname,template,sizeof(template)); + if ((fdtmp=mkstemp(tname)) >= 0) { + ftmp=fdopen(fdtmp,"wt"); + } else { + free(tname); + tname=NULL; + } /* if */ + } /* if */ + #endif + if (filename!=NULL) + *filename=tname; + return ftmp; +} + /* pc_closesrc() * Closes a source file (or include file). The "handle" parameter has the * value that pc_opensrc() returned in an earlier call. @@ -476,20 +513,7 @@ int pc_compile(int argc, char *argv[]) char *tname,*sname; FILE *ftmp,*fsrc; int fidx; - ftmp = NULL; - #if defined __WIN32__ || defined _WIN32 - tname=_tempnam(NULL,"pawn"); - #elif defined __MSDOS__ || defined _Windows - tname=tempnam(NULL,"pawn"); - #elif defined(__APPLE__) - int tfd; - tfd=mkstemp("/tmp/pawnXXXXXX"); - ftmp=fdopen(tfd, "wt"); - #else - tname=tempnam(NULL,"pawn"); - #endif - if (ftmp==NULL) - ftmp=(FILE*)pc_createsrc(tname); + ftmp=pc_createtmpsrc(&tname); for (fidx=0; (sname=get_sourcefile(fidx))!=NULL; fidx++) { unsigned char tstring[128]; fsrc=(FILE*)pc_opensrc(sname); From 6e8319d4f4e3f358eb14c3181e366047878871fc Mon Sep 17 00:00:00 2001 From: Zeex Date: Sat, 4 Apr 2015 17:55:55 +0600 Subject: [PATCH 5/7] Don't strip symbols Really, why do this? --- source/amx/CMakeLists.txt | 18 ------------------ source/compiler/CMakeLists.txt | 5 ----- 2 files changed, 23 deletions(-) diff --git a/source/amx/CMakeLists.txt b/source/amx/CMakeLists.txt index 62daa8f..f90fc94 100644 --- a/source/amx/CMakeLists.txt +++ b/source/amx/CMakeLists.txt @@ -50,9 +50,6 @@ if(WIN32) set_target_properties(amxArgs PROPERTIES LINK_FLAGS "/export:amx_ArgsInit /export:amx_ArgsCleanup /export:amx_ArgsSetCmdLine") endif() endif() -if(UNIX) - add_custom_command(TARGET amxArgs POST_BUILD COMMAND strip ARGS -K amx_ArgsInit -K amx_ArgsCleanup -K amx_ArgsSetCmdLine ${CMAKE_BINARY_DIR}/amxArgs.so) -endif() # amxDGram set(DGRAM_SRCS amxdgram.c amx.c) @@ -66,9 +63,6 @@ if(WIN32) set_target_properties(amxDGram PROPERTIES LINK_FLAGS "/export:amx_DGramInit /export:amx_DGramCleanup") endif() endif() -if(UNIX) - add_custom_command(TARGET amxDGram POST_BUILD COMMAND strip ARGS -K amx_DGramInit -K amx_DGramCleanup ${CMAKE_BINARY_DIR}/amxDGram.so) -endif() # amxFile set(FILE_SRCS amxfile.c amx.c) @@ -82,9 +76,6 @@ if(WIN32) set_target_properties(amxFile PROPERTIES LINK_FLAGS "/export:amx_FileInit /export:amx_FileCleanup") endif() endif() -if(UNIX) - add_custom_command(TARGET amxFile POST_BUILD COMMAND strip ARGS -K amx_FileInit -K amx_FileCleanup ${CMAKE_BINARY_DIR}/amxFile.so) -endif() # amxFixed set(FIXED_SRCS fixed.c amx.c) @@ -100,7 +91,6 @@ if(WIN32) endif() if(UNIX) target_link_libraries(amxFixed m) - add_custom_command(TARGET amxFixed POST_BUILD COMMAND strip ARGS -K amx_FixedInit -K amx_FixedCleanup ${CMAKE_BINARY_DIR}/amxFixed.so) endif() # amxFloat @@ -117,7 +107,6 @@ if(WIN32) endif() if(UNIX) target_link_libraries(amxFloat m) - add_custom_command(TARGET amxFloat POST_BUILD COMMAND strip ARGS -K amx_FloatInit -K amx_FloatInit ${CMAKE_BINARY_DIR}/amxFloat.so) endif() # amxProcess @@ -143,7 +132,6 @@ if(WIN32) endif() if(UNIX) target_link_libraries(amxProcess dl) - add_custom_command(TARGET amxProcess POST_BUILD COMMAND strip ARGS -K amx_ProcessInit -K amx_ProcessCleanup ${CMAKE_BINARY_DIR}/amxProcess.so) endif() # amxString @@ -158,9 +146,6 @@ if(WIN32) set_target_properties(amxString PROPERTIES LINK_FLAGS "/export:amx_StringInit /export:amx_StringCleanup") endif() endif() -if(UNIX) - add_custom_command(TARGET amxString POST_BUILD COMMAND strip ARGS -K amx_StringInit -K amx_StringCleanup ${CMAKE_BINARY_DIR}/amxString.so) -endif() # amxTime set(TIME_SRCS amxtime.c amx.c) @@ -174,9 +159,6 @@ if(WIN32) set_target_properties(amxTime PROPERTIES LINK_FLAGS "/export:amx_TimeInit /export:amx_TimeCleanup") endif() endif() -if(UNIX) - add_custom_command(TARGET amxTime POST_BUILD COMMAND strip ARGS -K amx_TimeInit -K amx_TimeCleanup ${CMAKE_BINARY_DIR}/amxTime.so) -endif() # -------------------------------------------------------------------------- # Simple run-time (example program) diff --git a/source/compiler/CMakeLists.txt b/source/compiler/CMakeLists.txt index 6531c9c..a2eb8a9 100644 --- a/source/compiler/CMakeLists.txt +++ b/source/compiler/CMakeLists.txt @@ -66,11 +66,6 @@ add_library(pawnc SHARED ${PAWNC_SRCS}) if(WATCOM) #Watcom C/C++ does not support a .DEF file for the exports set_target_properties(pawnc PROPERTIES LINK_FLAGS "/exp=libpawnc") endif() -if(UNIX AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug" - AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin") - # Unix exports all symbols by default; we want only a very select few exported symbols - add_custom_command(TARGET pawnc POST_BUILD COMMAND strip ARGS -K pc_compile -K pc_addconstant -K pc_addtag -K pc_enablewarning ${CMAKE_BINARY_DIR}/libpawnc.so) -endif() # The Pawn compiler driver (console program) set(PAWNCC_SRCS pawncc.c) From b57210fe805605d13c45d39b17374396ec5700fe Mon Sep 17 00:00:00 2001 From: Zeex Date: Sat, 4 Apr 2015 23:21:06 +0600 Subject: [PATCH 6/7] Detect strlcpy() and strlcat() via cmake --- source/compiler/CMakeLists.txt | 11 +++++++++++ source/compiler/lstring.c | 8 ++++++-- source/compiler/lstring.h | 11 ++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/source/compiler/CMakeLists.txt b/source/compiler/CMakeLists.txt index a2eb8a9..ab7d9fe 100644 --- a/source/compiler/CMakeLists.txt +++ b/source/compiler/CMakeLists.txt @@ -22,6 +22,17 @@ if(HAVE_ALLOCA_H) add_definitions(-DHAVE_ALLOCA_H) endif() +# check for optional library functions +include(CheckFunctionExists) +check_function_exists(strlcpy HAVE_STRLCPY) +if(HAVE_STRLCPY) + add_definitions(-DHAVE_STRLCPY) +endif() +check_function_exists(strlcat HAVE_STRLCAT) +if(HAVE_STRLCAT) + add_definitions(-DHAVE_STRLCAT) +endif() + if(MSVC) add_definitions(-D_CRT_SECURE_NO_WARNINGS /J) endif() diff --git a/source/compiler/lstring.c b/source/compiler/lstring.c index b21f352..bbd359f 100644 --- a/source/compiler/lstring.c +++ b/source/compiler/lstring.c @@ -7,7 +7,7 @@ #include "lstring.h" -#if !defined HAVE_SAFESTR +#if !defined HAVE_STRLCPY /* $OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */ @@ -63,6 +63,10 @@ strlcpy(char *dst, const char *src, size_t siz) return(s - src - 1); /* count does not include NUL */ } +#endif /* if !defined HAVE_STRLCPY */ + +#if !defined HAVE_STRLCAT + /* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */ /* @@ -121,4 +125,4 @@ strlcat(char *dst, const char *src, size_t siz) return(dlen + (s - src)); /* count does not include NUL */ } -#endif /* #if !defined HAVE_SAFESTR */ +#endif /* #if !defined HAVE_STRLCAT */ diff --git a/source/compiler/lstring.h b/source/compiler/lstring.h index 1e7e048..25c0873 100644 --- a/source/compiler/lstring.h +++ b/source/compiler/lstring.h @@ -7,11 +7,20 @@ #define HAVE_SAFESTR #endif -#if !defined HAVE_SAFESTR +#if defined HAVE_SAFESTR + #define HAVE_STRLCPY + #define HAVE_STRLCAT +#endif + +#if !defined HAVE_STRLCPY size_t strlcpy(char *dst, const char *src, size_t siz); +#endif + +#if !defined HAVE_STRLCAT + size_t strlcat(char *dst, const char *src, size_t siz); From 0a146f73448eaaa1c07df560bec1dc852fcb7ab0 Mon Sep 17 00:00:00 2001 From: Zeex Date: Sat, 4 Apr 2015 23:24:12 +0600 Subject: [PATCH 7/7] Set project language to C check_function_exists() tried to use a C++ compiler for some odd reason, so I've set the language to C explicitly. This shouldn't hurt in theory, since we don't use C++. --- source/amx/CMakeLists.txt | 2 +- source/compiler/CMakeLists.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/amx/CMakeLists.txt b/source/amx/CMakeLists.txt index f90fc94..608cbdb 100644 --- a/source/amx/CMakeLists.txt +++ b/source/amx/CMakeLists.txt @@ -1,4 +1,4 @@ -project(pawnamx) +project(pawnamx C) cmake_minimum_required(VERSION 2.8) # check for optional include files diff --git a/source/compiler/CMakeLists.txt b/source/compiler/CMakeLists.txt index ab7d9fe..6db9580 100644 --- a/source/compiler/CMakeLists.txt +++ b/source/compiler/CMakeLists.txt @@ -1,4 +1,4 @@ -project(pawnc) +project(pawnc C) cmake_minimum_required(VERSION 2.8) list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../cmake)