Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow XPMP2 to be built as a shared library #75

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 47 additions & 23 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
# 2. Define INCLUDE_FMOD_SOUND cache entry, e.g. using `cmake -G Ninja -D INCLUDE_FMOD_SOUND=1 ..`

cmake_minimum_required(VERSION 3.16)
include(GenerateExportHeader) #CMake helpers to generate export header

#Add our own cmake helper scripts for find_package(XPlaneSDK)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

# Mac: Need to tell early on that we want a cross platform build
if(DEFINED ENV{platform})
Expand All @@ -28,17 +32,23 @@ else()
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64" CACHE STRING "Archs to build")
endif()

option(XPMP2_BUILD_SHARED_LIBS "Build XPMP2 as a shared libraries" OFF)

project(XPMP2
VERSION 3.4.0
DESCRIPTION "Multiplayer library for X-Plane 11 and 12")

# Source list
add_library(XPMP2 STATIC
#Give hints to the find_package script
set(XPLANE_SDK_DIR "${CMAKE_CURRENT_SOURCE_DIR}/lib/SDK/")
find_package(XPlaneSDK REQUIRED)

set(SOURCE_FILES
inc/XPCAircraft.h
inc/XPMPAircraft.h
inc/XPMPMultiplayer.h
inc/XPMPRemote.h
inc/XPMPPlaneRenderer.h
inc/XPMP2Export.h
src/2D.h
src/2D.cpp
src/AIMultiplayer.h
Expand All @@ -62,8 +72,39 @@ add_library(XPMP2 STATIC
src/Utilities.h
src/Utilities.cpp
src/XPMP2.h
src/XPMPMultiplayer.cpp
)
src/XPMPMultiplayer.cpp)

# Source list
if(XPMP2_BUILD_SHARED_LIBS)
add_library(XPMP2 SHARED ${SOURCE_FILES})
target_compile_definitions(XPMP2 PUBLIC XPMP2_EXPORTS)
target_link_libraries(${PROJECT_NAME} PRIVATE wsock32 ws2_32 iphlpapi XPlaneSDK::XPLM XPlaneSDK::XPWidgets)
else()
# Debug vs Release build
if(CMAKE_BUILD_TYPE MATCHES "Debug")
target_compile_definitions(XPMP2 PRIVATE DEBUG=1)
if (MSVC)
target_compile_options(XPMP2 PRIVATE /Zi)
else()
target_compile_options(XPMP2 PRIVATE -O0 -g -fPIC)
endif()
else()
target_compile_definitions(XPMP2 PRIVATE NDEBUG=1)
if(MSVC)
# Fast code, symbols into separate .pdb file, no global optimization (as it produces huge files)
target_compile_options(XPMP2 PRIVATE /Zi /O2 /GL-)
else()
# Use position-independent code and highest optimization level
target_compile_options(XPMP2 PRIVATE -O3 -fPIC)
endif()
endif()
add_library(XPMP2 STATIC ${SOURCE_FILES})
target_compile_definitions(XPMP2 PUBLIC XPMP2_STATIC_DEFINE)
target_link_libraries(${PROJECT_NAME} PRIVATE XPlaneSDK::XPLM XPlaneSDK::XPWidgets)
endif()

#Generate the export header file. It is already part of the public header but we update it just in case
generate_export_header(XPMP2 EXPORT_FILE_NAME ${CMAKE_CURRENT_SOURCE_DIR}/inc/XPMP2Export.h)

# Provide compile macros from the above project version definition
target_compile_definitions(XPMP2 PRIVATE
Expand All @@ -74,6 +115,7 @@ target_compile_definitions(XPMP2 PRIVATE
)

message ("== Building: ${PROJECT_NAME} ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH} ==")
message ("Build Shared Lib = ${XPMP2_BUILD_SHARED_LIBS}")
message ("Compiler Info:")
message ("CMAKE_CXX_COMPILER_ID = ${CMAKE_CXX_COMPILER_ID}")
message ("CMAKE_CXX_COMPILER_VERSION = ${CMAKE_CXX_COMPILER_VERSION}")
Expand Down Expand Up @@ -138,25 +180,6 @@ else()
target_compile_options(XPMP2 PRIVATE -fvisibility=hidden)
endif()

# Debug vs Release build
if(CMAKE_BUILD_TYPE MATCHES "Debug")
target_compile_definitions(XPMP2 PRIVATE DEBUG=1)
if (MSVC)
target_compile_options(XPMP2 PRIVATE /Zi)
else()
target_compile_options(XPMP2 PRIVATE -O0 -g -fPIC)
endif()
else()
target_compile_definitions(XPMP2 PRIVATE NDEBUG=1)
if(MSVC)
# Fast code, symbols into separate .pdb file, no global optimization (as it produces huge files)
target_compile_options(XPMP2 PRIVATE /Zi /O2 /GL-)
else()
# Use position-independent code and highest optimization level
target_compile_options(XPMP2 PRIVATE -O3 -fPIC)
endif()
endif()

# Mingw Threads (if not already defined by an outer target)
if (MINGW AND NOT TARGET mingw_stdthreads)
option(MINGW_STDTHREADS_GENERATE_STDHEADERS "" ON)
Expand Down Expand Up @@ -212,6 +235,7 @@ if(APPLE)
../inc/XPMPMultiplayer.h
../inc/XPMPPlaneRenderer.h
../inc/XPMPRemote.h
../inc/XPMP2Export.h
)

set_target_properties(XPMP2 PROPERTIES
Expand Down
92 changes: 92 additions & 0 deletions cmake/FindXPlaneSDK.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# FindXPlaneSDK.cmake

# Allow the user to set the path manually
set(XPLANE_SDK_DIR "" CACHE PATH "Path to the X-Plane SDK")

# Define common search paths for SDK
if(NOT XPLANE_SDK_DIR)
if(WIN32)
set(_xplane_sdk_possible_paths
"C:/XPlaneSDK"
"C:/Program Files/XPlaneSDK"
)
elseif(APPLE)
set(_xplane_sdk_possible_paths
"/Library/Developer/XPlaneSDK"
"$ENV{HOME}/XPlaneSDK"
)
elseif(UNIX)
set(_xplane_sdk_possible_paths
"/usr/local/XPlaneSDK"
"/opt/XPlaneSDK"
)
endif()

# Search for the SDK root directory
find_path(XPLANE_SDK_DIR
NAMES XPLM/XPLM.h
PATHS ${_xplane_sdk_possible_paths}
DOC "Directory containing the X-Plane SDK"
)
endif()

# If not found, display an error message
if(NOT XPLANE_SDK_DIR)
message(FATAL_ERROR "X-Plane SDK not found. Please set XPLANE_SDK_DIR.")
endif()

# Include directories for the X-Plane SDK
set(XPLANE_SDK_INCLUDE_DIR ${XPLANE_SDK_DIR}/CHeaders)

# Define common search paths for libraries
set(_xplane_lib_xplm_name "XPLM")
set(_xplane_lib_xpwidgets_name "XPWidgets")
if(WIN32)
set(_xplane_lib_possible_paths
"${XPLANE_SDK_DIR}/Libraries/Win"
)
set(_xplane_lib_xplm_name "XPLM_64")
set(_xplane_lib_xpwidgets_name "XPWidgets_64")
elseif(APPLE)
set(_xplane_lib_possible_paths
"${XPLANE_SDK_DIR}/Libraries/Mac"
)
elseif(UNIX)
set(_xplane_lib_possible_paths
"${XPLANE_SDK_DIR}/Libraries/Linux"
)
endif()

# Search for the XPLM and XPWidgets libraries
find_library(XPLM_LIBRARY NAMES ${_xplane_lib_xplm_name} PATHS ${_xplane_lib_possible_paths})
find_library(XPWIDGETS_LIBRARY NAMES ${_xplane_lib_xpwidgets_name} PATHS ${_xplane_lib_possible_paths})

# If the libraries are not found, display an error
if(NOT XPLM_LIBRARY)
message(FATAL_ERROR "Failed to find XPLM library.")
endif()

if(NOT XPWIDGETS_LIBRARY)
message(FATAL_ERROR "Failed to find XPWidgets library.")
endif()

# Provide the results to the parent scope
set(XPLANE_SDK_INCLUDE_DIR ${XPLANE_SDK_INCLUDE_DIR})
set(XPLANE_SDK_LIBRARIES ${XPLM_LIBRARY} ${XPWIDGETS_LIBRARY})

# Optionally, create an imported target for modern CMake usage
if(NOT TARGET XPlaneSDK::XPLM)
add_library(XPlaneSDK::XPLM UNKNOWN IMPORTED)
set_target_properties(XPlaneSDK::XPLM PROPERTIES
IMPORTED_LOCATION ${XPLM_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES ${XPLANE_SDK_INCLUDE_DIR}
)
endif()

if(NOT TARGET XPlaneSDK::XPWidgets)
add_library(XPlaneSDK::XPWidgets UNKNOWN IMPORTED)
set_target_properties(XPlaneSDK::XPWidgets PROPERTIES
IMPORTED_LOCATION ${XPWIDGETS_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES ${XPLANE_SDK_INCLUDE_DIR}
)
endif()
42 changes: 42 additions & 0 deletions inc/XPMP2Export.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

#ifndef XPMP2_EXPORT_H
#define XPMP2_EXPORT_H

#ifdef XPMP2_STATIC_DEFINE
# define XPMP2_EXPORT
# define XPMP2_NO_EXPORT
#else
# ifndef XPMP2_EXPORT
# ifdef XPMP2_EXPORTS
/* We are building this library */
# define XPMP2_EXPORT __declspec(dllexport)
# else
/* We are using this library */
# define XPMP2_EXPORT __declspec(dllimport)
# endif
# endif

# ifndef XPMP2_NO_EXPORT
# define XPMP2_NO_EXPORT
# endif
#endif

#ifndef XPMP2_DEPRECATED
# define XPMP2_DEPRECATED __declspec(deprecated)
#endif

#ifndef XPMP2_DEPRECATED_EXPORT
# define XPMP2_DEPRECATED_EXPORT XPMP2_EXPORT XPMP2_DEPRECATED
#endif

#ifndef XPMP2_DEPRECATED_NO_EXPORT
# define XPMP2_DEPRECATED_NO_EXPORT XPMP2_NO_EXPORT XPMP2_DEPRECATED
#endif

#if 0 /* DEFINE_NO_DEPRECATED */
# ifndef XPMP2_NO_DEPRECATED
# define XPMP2_NO_DEPRECATED
# endif
#endif

#endif /* XPMP2_EXPORT_H */
4 changes: 2 additions & 2 deletions inc/XPMPAircraft.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ enum DR_VALS : std::uint8_t {
/// as it contains many technical implementation details.
/// This structure contains some of the CSLModel information in a public
/// definition, returned by XPMP2::Aircraft::GetModelInfo().
struct CSLModelInfo_t {
struct XPMP2_EXPORT CSLModelInfo_t {
/// id, just an arbitrary label read from `xsb_aircraft.txt::OBJ8_AIRCRAFT`
std::string cslId;
/// name, formed by last part of path plus id
Expand Down Expand Up @@ -175,7 +175,7 @@ struct CSLModelInfo_t {

/// @brief Actual representation of all aircraft in XPMP2.
/// @note In modern implementations, this class shall be subclassed by your plugin's code.
class Aircraft {
class XPMP2_EXPORT Aircraft {

public:

Expand Down
Loading