Skip to content

Commit

Permalink
Move platform macros to FMI.h (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-sommer authored May 17, 2024
1 parent 069c79b commit bfa9cc3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 82 deletions.
44 changes: 2 additions & 42 deletions examples/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,49 +28,9 @@
#define str(s) #s

#if FMI_VERSION == 1 || FMI_VERSION == 2

#if defined(_WIN32)
#if defined(__x86_64__) || defined(_M_X64)
#define PLATFORM_BINARY xstr(MODEL_IDENTIFIER) "\\binaries\\win64\\" xstr(MODEL_IDENTIFIER) ".dll"
#else
#define PLATFORM_BINARY xstr(MODEL_IDENTIFIER) "\\binaries\\win32\\" xstr(MODEL_IDENTIFIER) ".dll"
#endif
#elif defined(__APPLE__)
#define PLATFORM_BINARY xstr(MODEL_IDENTIFIER) "/binaries/darwin64/" xstr(MODEL_IDENTIFIER) ".dylib"
#else
#define PLATFORM_BINARY xstr(MODEL_IDENTIFIER) "/binaries/linux64/" xstr(MODEL_IDENTIFIER) ".so"
#endif

#else

#if defined(__aarch64__) || defined(_M_ARM64)
#define ARCH "aarch64"
#elif defined(__x86_64__) || defined(_M_X64)
#define ARCH "x86_64"
#elif defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
#define ARCH "x86"
#else
#error "Unknown architecture"
#endif

#if defined(_WIN32)
#define SYS "windows"
#define SEP "\\"
#define EXT ".dll"
#elif defined(__APPLE__)
#define SYS "darwin"
#define SEP "/"
#define EXT ".dylib"
#elif defined(__linux__)
#define SYS "linux"
#define SEP "/"
#define EXT ".so"
#define PLATFORM_BINARY xstr(MODEL_IDENTIFIER) FMI_FILE_SEPARATOR "binaries" FMI_FILE_SEPARATOR FMI_PLATFORM FMI_FILE_SEPARATOR xstr(MODEL_IDENTIFIER) FMI_SHARED_LIBRARY_EXTENSION
#else
#error "Unknown platform"
#endif

#define PLATFORM_BINARY xstr(MODEL_IDENTIFIER) SEP "binaries" SEP ARCH "-" SYS SEP xstr(MODEL_IDENTIFIER) EXT

#define PLATFORM_BINARY xstr(MODEL_IDENTIFIER) FMI_FILE_SEPARATOR "binaries" FMI_FILE_SEPARATOR FMI_PLATFORM_TUPLE FMI_FILE_SEPARATOR xstr(MODEL_IDENTIFIER) FMI_SHARED_LIBRARY_EXTENSION
#endif

#ifndef min
Expand Down
36 changes: 36 additions & 0 deletions include/FMI.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,42 @@ extern "C" {
#define FMI_STATIC
#endif

#if defined(_WIN32)
#define FMI_SYSTEM "windows"
#define FMI_PLATFORM_SYSTEM "win"
#define FMI_FILE_SEPARATOR "\\"
#define FMI_SHARED_LIBRARY_EXTENSION ".dll"
#elif defined(__APPLE__)
#define FMI_SYSTEM "darwin"
#define FMI_PLATFORM_SYSTEM "darwin"
#define FMI_FILE_SEPARATOR "/"
#define FMI_SHARED_LIBRARY_EXTENSION ".dylib"
#elif defined(__linux__)
#define FMI_SYSTEM "linux"
#define FMI_PLATFORM_SYSTEM "linux"
#define FMI_FILE_SEPARATOR "/"
#define FMI_SHARED_LIBRARY_EXTENSION ".so"
#else
#error "Unknown system"
#endif

#if defined(__aarch64__) || defined(_M_ARM64)
#define FMI_ARCHITECTURE "aarch64"
#define FMI_PLATFORM_BITS "64"
#elif defined(__x86_64__) || defined(_M_X64)
#define FMI_ARCHITECTURE "x86_64"
#define FMI_PLATFORM_BITS "64"
#elif defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
#define FMI_ARCHITECTURE "x86"
#define FMI_PLATFORM_BITS "32"
#else
#error "Unknown architecture"
#endif

#define FMI_PLATFORM FMI_PLATFORM_SYSTEM FMI_PLATFORM_BITS
#define FMI_PLATFORM_TUPLE FMI_ARCHITECTURE "-" FMI_SYSTEM


typedef enum {
FMIOK,
FMIWarning,
Expand Down
48 changes: 8 additions & 40 deletions src/FMI.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,50 +393,18 @@ FMIStatus FMIPathToURI(const char *path, char *uri, const size_t uriLength) {

FMIStatus FMIPlatformBinaryPath(const char *unzipdir, const char *modelIdentifier, FMIVersion fmiVersion, char *platformBinaryPath, size_t size) {

#if defined(_WIN32)
const char *platform = "win";
const char *system = "windows";
const char sep = '\\';
const char *ext = ".dll";
#elif defined(__APPLE__)
const char *platform = "darwin";
const char *system = "darwin";
const char sep = '/';
const char *ext = ".dylib";
#elif defined(__linux__)
const char *platform = "linux";
const char *system = "linux";
const char sep = '/';
const char *ext = ".so";
#else
#error "Unknown platform"
#endif
char* separator = ""; // optional separator after the unzipdir

#if defined(__aarch64__) || defined(_M_ARM64)
const char* bits = "64";
const char* arch = "aarch64";
#elif defined(__x86_64__) || defined(_M_X64)
const char *bits = "64";
const char *arch = "x86_64";
#elif defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
const char *bits = "32";
const char *arch = "x86";
#else
#error "Unknown architecture"
#endif
const char* bin = "binaries";
char optSep[2] = "";
int rc;
const char last = unzipdir[strlen(unzipdir) - 1];

if (unzipdir[strlen(unzipdir) - 1] != sep) {
optSep[0] = sep;
if (last != '/' && last != '\\') {
separator = FMI_FILE_SEPARATOR;
}

if (fmiVersion == 3) {
rc = snprintf(platformBinaryPath, size, "%s%s%s%c%s-%s%c%s%s", unzipdir, optSep, bin, sep, arch, system, sep, modelIdentifier, ext);
} else {
rc = snprintf(platformBinaryPath, size, "%s%s%s%c%s%s%c%s%s", unzipdir, optSep, bin, sep, platform, bits, sep, modelIdentifier, ext);
}
const char* platform = fmiVersion < FMIVersion3 ? FMI_PLATFORM : FMI_PLATFORM_TUPLE;

const int rc = snprintf(platformBinaryPath, size, "%s%sbinaries" FMI_FILE_SEPARATOR "%s" FMI_FILE_SEPARATOR "%s" FMI_SHARED_LIBRARY_EXTENSION,
unzipdir, separator, platform, modelIdentifier);

if (rc >= size) {
return FMIError;
Expand Down

0 comments on commit bfa9cc3

Please sign in to comment.