Skip to content

Commit 33b78a5

Browse files
committed
refactor: Link amxxpc32 library statically
1 parent 9b34c24 commit 33b78a5

File tree

8 files changed

+75
-79
lines changed

8 files changed

+75
-79
lines changed

CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,6 @@ include("cmake/ClangTidy.cmake")
167167
include("cmake/Cppcheck.cmake")
168168
include("cmake/PvsStudio.cmake")
169169

170-
if(UNIX)
171-
find_package(Threads REQUIRED)
172-
endif()
173-
174170
#-------------------------------------------------------------------------------
175171
# Subdirectories
176172
#-------------------------------------------------------------------------------

apps/amxxpc/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ target_compile_definitions("${TARGET_NAME}"
8787
AMX_ANSIONLY
8888
HAVE_STDINT_H
8989

90+
$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:
91+
BUILD_STATIC_AMXXPC
92+
>
93+
9094
$<$<PLATFORM_ID:Windows>:
9195
_MBCS
9296
_CRT_SECURE_NO_WARNINGS

apps/amxxpc/src/amx.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
#endif
3636

3737
#include "osdefs.h"
38-
#include <assert.h>
39-
#include <limits.h>
40-
#include <stdarg.h>
41-
#include <stddef.h> /* for wchar_t */
42-
#include <string.h>
38+
#include <cassert>
39+
#include <climits>
40+
#include <cstdarg>
41+
#include <cstddef> /* for wchar_t */
42+
#include <cstring>
4343
#if defined LINUX || defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__
4444
#include "sclinux.h"
4545
#if !defined AMX_NODYNALOAD

apps/amxxpc/src/amxdbg.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#ifndef AMXDBG_H_INCLUDED
2626
#define AMXDBG_H_INCLUDED
2727

28+
#include <cstdio>
29+
2830
#ifndef AMX_H_INCLUDED
2931
#include "amx.h"
3032
#endif

apps/amxxpc/src/amxxpc.cpp

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
88
// https://alliedmods.net/amxmodx-license
99

10-
#include <stdio.h>
11-
#if defined(__linux__) | defined(__APPLE__)
10+
#if defined(__linux__) || defined(__APPLE__)
1211
#include <unistd.h>
1312
#else
1413
#include <fcntl.h>
@@ -19,7 +18,20 @@
1918
#include "amxxpc.h"
2019
#include "binary.h"
2120
#include "zlib.h"
22-
#include <stdlib.h>
21+
#include <cstdio>
22+
#include <cstdlib>
23+
24+
#ifdef BUILD_STATIC_AMXXPC
25+
#include <sc.h>
26+
extern "C" void Compile32(int argc, char** argv);
27+
#else
28+
#if defined(EMSCRIPTEN)
29+
extern "C" void Compile32(int argc, char** argv);
30+
extern "C" int pc_printf(const char* message, ...);
31+
#else
32+
static PRINTF pc_printf = nullptr;
33+
#endif
34+
#endif
2335

2436
#ifdef _MSC_VER
2537
// MSVC8 - replace POSIX functions with ISO C++ conformant ones as they are deprecated
@@ -33,54 +45,51 @@ bool CompressPl(abl* pl);
3345
void Pl2Bh(const abl* pl, BinPlugin* bh);
3446
void WriteBh(const BinaryWriter* bw, const BinPlugin* bh);
3547

36-
#if defined(EMSCRIPTEN)
37-
extern "C" void Compile32(int argc, char** argv);
38-
extern "C" int pc_printf(const char* message, ...);
39-
#else
40-
static PRINTF pc_printf = nullptr;
41-
#endif
42-
4348
int main(const int argc, char** argv)
4449
{
4550
abl pl32;
4651

47-
#if defined(EMSCRIPTEN)
52+
#ifndef BUILD_STATIC_AMXXPC
53+
#if defined(EMSCRIPTEN)
4854
COMPILER sc32 = (COMPILER)Compile32;
49-
#else
50-
#if defined(__linux__)
55+
#else
56+
#if defined(__linux__)
5157
HINSTANCE lib = NULL;
5258
if (FileExists("./amxxpc32.so")) {
5359
lib = dlmount("./amxxpc32.so");
5460
}
5561
else {
5662
lib = dlmount("amxxpc32.so");
5763
}
58-
#elif defined(__APPLE__)
64+
#elif defined(__APPLE__)
5965
HINSTANCE lib = dlmount("amxxpc32.dylib");
60-
#else
66+
#else
6167
const HINSTANCE lib = dlmount("amxxpc32.dll");
62-
#endif
68+
#endif
6369
if (!lib) {
64-
#if defined(__linux__) || defined(__APPLE__)
70+
#if defined(__linux__) || defined(__APPLE__)
6571
printf("compiler failed to instantiate: %s\n", dlerror());
66-
#else
72+
#else
6773
printf("compiler failed to instantiate: %d\n", GetLastError());
68-
#endif
74+
#endif
6975
exit(EXIT_FAILURE);
7076
}
7177

7278
const auto sc32 = (COMPILER)dlsym(lib, "Compile32");
7379
pc_printf = (PRINTF)dlsym(lib, "pc_printf");
74-
#endif // EMSCRIPTEN
80+
#endif // EMSCRIPTEN
7581

7682
if (!sc32 || !pc_printf) {
77-
#if defined(__linux__) || defined(__APPLE__)
83+
#if defined(__linux__) || defined(__APPLE__)
7884
printf("compiler failed to link: %p.\n", sc32);
79-
#else
85+
#else
8086
printf("compiler failed to link: %d.\n", GetLastError());
81-
#endif
87+
#endif
8288
exit(EXIT_FAILURE);
8389
}
90+
#else
91+
const auto sc32 = (COMPILER)Compile32;
92+
#endif
8493

8594
pc_printf("AMX Mod X Compiler %s\n\n", AMXX_VERSION);
8695
pc_printf("Copyright (c) 1997-2006 ITB CompuPhase\n");
@@ -175,7 +184,7 @@ int main(const int argc, char** argv)
175184
fclose(fp);
176185
unlink(file);
177186
pc_printf("Error, failed to write binary\n");
178-
#if !defined EMSCRIPTEN
187+
#if !defined(BUILD_STATIC_AMXXPC) && !defined(EMSCRIPTEN)
179188
dlclose(lib);
180189
#endif
181190
exit(EXIT_FAILURE);
@@ -190,7 +199,7 @@ int main(const int argc, char** argv)
190199
and "Compile and upload" buttons in AMXX-Studio doesn't work.
191200
*/
192201
pc_printf("Done.\n");
193-
#if !defined EMSCRIPTEN
202+
#if !defined(BUILD_STATIC_AMXXPC) && !defined(EMSCRIPTEN)
194203
dlclose(lib);
195204
#endif
196205

apps/amxxpc/src/sclinux.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#if defined EMSCRIPTEN
3535
#include <endian.h>
3636
#else
37-
#include <stdlib.h>
37+
#include <cstdlib>
3838
#endif
3939
#if defined __APPLE__
4040
#include <sys/types.h>

libs/amxxpc32/CMakeLists.txt

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ project("AMXX Nova Pawn Compiler Lib" LANGUAGES "C")
1111
set(TARGET_NAME "amxxpc32")
1212
set(TARGET_ALIAS "PCLib::${TARGET_NAME}")
1313

14-
add_library("${TARGET_NAME}" SHARED)
14+
add_library("${TARGET_NAME}")
1515
add_library("${TARGET_ALIAS}" ALIAS "${TARGET_NAME}")
1616

1717
#-------------------------------------------------------------------------------
@@ -65,10 +65,13 @@ set_target_properties("${TARGET_NAME}"
6565
target_compile_definitions("${TARGET_NAME}"
6666
PRIVATE
6767
NO_MAIN
68-
PAWNC_DLL
6968
HAVE_STDINT_H
7069
PAWN_CELL_SIZE=32
7170

71+
$<$<BOOL:${BUILD_SHARED_LIBS}>:
72+
PAWNC_DLL
73+
>
74+
7275
$<$<PLATFORM_ID:Windows>:
7376
_MBCS
7477
_CRT_SECURE_NO_WARNINGS
@@ -80,16 +83,3 @@ target_compile_definitions("${TARGET_NAME}"
8083
ENABLE_BINRELOC
8184
>
8285
)
83-
84-
#-------------------------------------------------------------------------------
85-
# Link Libraries
86-
#-------------------------------------------------------------------------------
87-
88-
target_link_libraries("${TARGET_NAME}"
89-
PRIVATE
90-
$<$<NOT:$<PLATFORM_ID:Windows>>:
91-
"c"
92-
"m"
93-
"Threads::Threads"
94-
>
95-
)

libs/amxxpc32/src/libpawnc.c

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,51 +29,46 @@
2929
#include <string.h>
3030

3131
#if defined PAWNC_DLL
32+
#if defined _MSC_VER
33+
#define AMXXPC_API __declspec(dllexport)
34+
#else
35+
#define AMXXPC_API __attribute__((visibility("default")))
36+
#endif
3237

3338
#if !defined(__linux__) && !defined(__APPLE__)
3439
#include "dllmain.c"
3540
#endif
41+
#else
42+
#define AMXXPC_API
43+
#endif
3644

37-
#define MAX_ARGS 100
38-
#if !defined UNUSED_PARAM
39-
#define UNUSED_PARAM(p) ((void)(p))
40-
#endif
41-
42-
#if PAWN_CELL_SIZE == 32
43-
#define EXCOMPILER Compile32
44-
#else
45-
#define EXCOMPILER Compile64
46-
#endif
47-
48-
#if defined __WIN32__ || defined _WIN32 || defined WIN32 || defined __NT__
49-
__declspec(dllexport) void EXCOMPILER(const int argc, char** argv)
50-
#else
51-
void extern __attribute__((visibility("default"))) EXCOMPILER(const int argc, char** argv)
52-
#endif
53-
{
54-
pc_compile(argc, argv);
55-
}
56-
#endif /* PAWNC_DLL */
45+
#define MAX_ARGS 100
46+
#if !defined UNUSED_PARAM
47+
#define UNUSED_PARAM(p) ((void)(p))
48+
#endif
5749

5850
#if defined LINUX || defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__
5951
#include <sys/stat.h>
6052
#include <sys/types.h>
6153
#endif
6254

55+
#if PAWN_CELL_SIZE == 32
56+
#define EXCOMPILER Compile32
57+
#else
58+
#define EXCOMPILER Compile64
59+
#endif
60+
61+
AMXXPC_API void EXCOMPILER(const int argc, char** argv)
62+
{
63+
pc_compile(argc, argv);
64+
}
65+
6366
/* pc_printf()
6467
* Called for general purpose "console" output. This function prints general
6568
* purpose messages; errors go through pc_error(). The function is modelled
6669
* after printf().
6770
*/
68-
#if PAWN_CELL_SIZE == 32
69-
#if defined __WIN32__ || defined _WIN32 || defined WIN32
70-
__declspec(dllexport) int pc_printf(const char* message, ...)
71-
#else
72-
extern int __attribute__((visibility("default"))) pc_printf(const char* message, ...)
73-
#endif
74-
#else
75-
int pc_printf(const char* message, ...)
76-
#endif
71+
AMXXPC_API int pc_printf(const char* message, ...)
7772
{
7873
#if PAWN_CELL_SIZE == 32
7974
va_list argptr;

0 commit comments

Comments
 (0)