Skip to content

Commit

Permalink
Windows build fixes
Browse files Browse the repository at this point in the history
- Fix conflicting interface keywork in MinGW
- Improve build system
  • Loading branch information
wardru committed Oct 1, 2023
1 parent 6f41c5e commit 4daf993
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ examples/CMakeUserPresets.json
CMakeUserPresets.json
.vscode
Testing
conan_imports_manifest.txt
31 changes: 23 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,25 @@ set(SPDLOG_FMT_EXTERNAL 1)
find_package(spdlog REQUIRED)
find_package(nlohmann_json REQUIRED)
find_package(PahoMqttCpp REQUIRED)
find_package(GTest REQUIRED)
find_package(cppcheck REQUIRED)
find_package(magic_enum REQUIRED)

if (CMAKE_SYSTEM_NAME MATCHES "Windows" OR CMAKE_SYSTEM_NAME MATCHES "Linux" AND NOT BUILD_SHARED_LIBS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-allow-multiple-definition")
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
find_package(GTest REQUIRED)
find_package(cppcheck REQUIRED)
endif()

if (CMAKE_SYSTEM_NAME MATCHES "Windows")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")
set(CMAKE_CROSSCOMPILING_EMULATOR "wine")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--allow-multiple-definition")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--allow-multiple-definition")
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux" AND NOT BUILD_SHARED_LIBS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--allow-multiple-definition")
endif()

if (CMAKE_SYSTEM_NAME MATCHES "Windows" AND NOT BUILD_SHARED_LIBS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
endif()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
add_library(${LIBRARY_NAME})
Expand All @@ -44,13 +51,21 @@ target_include_directories(${LIBRARY_NAME} PUBLIC
)

target_link_libraries(${LIBRARY_NAME}
$<$<OR:$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>,$<STREQUAL:${CMAKE_SYSTEM_NAME},Windows>>:PahoMqttCpp::paho-mqttpp3-static>
$<$<AND:$<BOOL:${BUILD_SHARED_LIBS}>,$<NOT:$<STREQUAL:${CMAKE_SYSTEM_NAME},Windows>>>:PahoMqttCpp::paho-mqttpp3>
$<$<BOOL:${BUILD_SHARED_LIBS}>:PahoMqttCpp::paho-mqttpp3>
$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:PahoMqttCpp::paho-mqttpp3-static>
spdlog::spdlog
nlohmann_json::nlohmann_json
magic_enum::magic_enum
)

if (CMAKE_SYSTEM_NAME MATCHES "Windows" AND BUILD_SHARED_LIBS)
add_custom_command(TARGET ${LIBRARY_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_BINARY_DIR}/bin/*.dll
$<TARGET_FILE_DIR:${LIBRARY_NAME}>
)
endif()

set_target_properties(${LIBRARY_NAME} PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
Expand Down
26 changes: 13 additions & 13 deletions cmake/cppcheck.cmake
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Add a target for cppcheck
find_program(CPPCHECK_EXECUTABLE cppcheck)
if(CPPCHECK_EXECUTABLE)
# cppcheck igner never used functions
add_custom_target(check
${CPPCHECK_EXECUTABLE} --enable=all --suppress=missingIncludeSystem --suppress=unusedFunction
--template "{file}:{line}:{severity}:{id}:{message}"
-I ${CMAKE_SOURCE_DIR}/source
${CMAKE_SOURCE_DIR}/source
COMMENT "Running Cppcheck static analysis tool"
)
else()
message(WARNING "Cppcheck not found, can't run static analysis")
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
find_program(CPPCHECK_EXECUTABLE cppcheck)
if(CPPCHECK_EXECUTABLE)
add_custom_target(check
${CPPCHECK_EXECUTABLE} --enable=all --suppress=missingIncludeSystem --suppress=unusedFunction
--template "{file}:{line}:{severity}:{id}:{message}"
-I ${CMAKE_SOURCE_DIR}/source
${CMAKE_SOURCE_DIR}/source
COMMENT "Running Cppcheck static analysis tool"
)
else()
message(WARNING "Cppcheck not found, can't run static analysis")
endif()
endif()
11 changes: 5 additions & 6 deletions conan_profiles/x86_64_Cross_Windows
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
$toolchain=/usr/x86_64-w64-mingw32 # Adjust this path
target_host=x86_64-w64-mingw32
cc_compiler=gcc
cxx_compiler=g++

[env]
CONAN_CMAKE_FIND_ROOT_PATH=$toolchain
CHOST=$target_host
AR=$target_host-ar
AS=$target_host-as
RANLIB=$target_host-ranlib
CC=$target_host-$cc_compiler
CXX=$target_host-$cxx_compiler
CC=$target_host-gcc
CXX=$target_host-g++
STRIP=$target_host-strip
RC=$target_host-windres

[conf]
tools.build:compiler_executables={"cpp": "$target_host-g++", "c": "$target_host-gcc"}

# We are cross building to Windows
[settings]
os=Windows
Expand Down
7 changes: 0 additions & 7 deletions conan_profiles/x86_64_Linux
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
[settings]
os=Linux
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=13
compiler.libcxx=libstdc++11
build_type=Release
[options]
[build_requires]
[env]

22 changes: 15 additions & 7 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ def requirements(self):
self.requires("paho-mqtt-cpp/[>=1.2.0]")
self.requires("spdlog/[>=1.11.0]")
self.requires("nlohmann_json/[>=3.11.2]")
self.requires("gtest/cci.20210126")
self.requires("cppcheck/[>=2.10]")
self.requires("magic_enum/[>=0.9.2]")
if self.settings.os == "Linux":
self.requires("gtest/cci.20210126")
self.requires("cppcheck/[>=2.10]")

def layout(self):
cmake_layout(self, build_folder=os.getcwd())

def configure(self):
if self.settings.os == "Windows":
self.options["*"].shared = False
else:
self.options["*"].shared = self.options.shared
self.options["*"].shared = self.options.shared

def generate(self):
tc = CMakeToolchain(self)
Expand All @@ -49,4 +47,14 @@ def build(self):

def package(self):
cmake = CMake(self)
cmake.install()
cmake.install()

def imports(self):
if self.settings.os == "Windows" and self.options.shared:
folder = f"{self.build_folder}/bin"
self.copy("*.dll", dst=folder, src="bin")
mingw_dlls = ["libgcc_s_seh-1.dll", "libwinpthread-1.dll", "libstdc++-6.dll"]
mingw_dll_path = "/usr/x86_64-w64-mingw32/bin"
for dll in mingw_dlls:
self.copy(dll, dst=folder, src=mingw_dll_path)

11 changes: 9 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ set(examples
foreach(example ${examples})
add_executable(${example} ${example}.cxx)
set_target_properties(${example} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/examples"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/examples/bin"
)
target_link_libraries(${example} ${LIBRARY_NAME})
endforeach()
if (CMAKE_SYSTEM_NAME MATCHES "Windows" AND BUILD_SHARED_LIBS)
add_custom_command(TARGET ${example} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_BINARY_DIR}/bin/*.dll
$<TARGET_FILE_DIR:${example}>
)
endif()
endforeach()
4 changes: 3 additions & 1 deletion examples/bps.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ int main(void)

//bps_channel->voltmeter.set_measure_polling_cycle(2);
//bps_channel->ampermeter.set_measure_polling_cycle(2);
bps_channel->ctrl.set_enable_polling_cycle(2);
//bps_channel->ctrl.set_enable_polling_cycle(2);
}

spdlog::info("\n\nOK\n\n");

cli->disconnect();

return 0;
}
2 changes: 2 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ function install_deps {
echo "Installing dependencies for $TARGET..."
if [ "$TARGET" = "Windows" ]; then
EXTRA_CONAN_ARGS="-pr:h ../conan_profiles/x86_64_Cross_Windows"
else
EXTRA_CONAN_ARGS="-pr:h ../conan_profiles/x86_64_Linux"
fi
mkdir -p $BUILD_DIR
cd $BUILD_DIR
Expand Down
2 changes: 1 addition & 1 deletion source/pza/core/attribute.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace pza
class attribute
{
public:
friend class interface;
friend class itface;

explicit attribute(const std::string &name);

Expand Down
2 changes: 1 addition & 1 deletion source/pza/core/client.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace pza
using ptr = std::shared_ptr<client>;

friend class device;
friend class interface;
friend class itface;

explicit client(const std::string &addr, int port, const std::string &id = "");

Expand Down
4 changes: 2 additions & 2 deletions source/pza/core/device.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int device::_set_identity(const std::string &payload)
return 0;
}

void device::register_interface(interface &interface)
void device::register_interface(itface &itface)
{
_interfaces[interface._name] = &interface;
_interfaces[itface._name] = &itface;
}
6 changes: 3 additions & 3 deletions source/pza/core/device.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace pza
using ptr = std::shared_ptr<device>;

friend class client;
friend class interface;
friend class itface;

enum class state : unsigned int
{
Expand All @@ -37,7 +37,7 @@ namespace pza

void reset();
enum state get_state() { return _state; }
void register_interface(interface &interface);
void register_interface(itface &itface);

protected:
device(const std::string &group, const std::string &name);
Expand All @@ -58,7 +58,7 @@ namespace pza
std::string _base_topic;
std::string _device_topic;

std::map<std::string, interface*> _interfaces;
std::map<std::string, itface*> _interfaces;

enum state _state = state::orphan;
};
Expand Down
6 changes: 3 additions & 3 deletions source/pza/core/interface.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

using namespace pza;

interface::interface(device *device, const std::string &name)
itface::itface(device *device, const std::string &name)
: _device(device),
_name(name)
{
_topic_base = _device->_get_base_topic() + "/" + _name;
_topic_cmd = _topic_base + "/cmds/set";
}

void interface::register_attribute(attribute &attribute)
void itface::register_attribute(attribute &attribute)
{
std::condition_variable cv;
std::unique_lock<std::mutex> lock(_mtx);
Expand All @@ -38,7 +38,7 @@ void interface::register_attribute(attribute &attribute)
}
}

void interface::register_attributes(const std::vector<attribute*> &list)
void itface::register_attributes(const std::vector<attribute*> &list)
{
for (auto const &it : list) {
register_attribute(*it);
Expand Down
4 changes: 2 additions & 2 deletions source/pza/core/interface.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace pza
{
class device;

class interface
class itface
{
public:
friend class device;

interface(device *device, const std::string &name);
itface(device *device, const std::string &name);

void register_attribute(attribute &attribute);
void register_attributes(const std::vector<attribute*> &list);
Expand Down
2 changes: 1 addition & 1 deletion source/pza/interfaces/bps_chan_ctrl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using namespace pza;

bps_chan_ctrl::bps_chan_ctrl(device *device, const std::string &name)
: interface(device, name),
: itface(device, name),
_volts("volts"),
_amps("amps"),
_enable("enable")
Expand Down
2 changes: 1 addition & 1 deletion source/pza/interfaces/bps_chan_ctrl.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace pza
{
class device;

class bps_chan_ctrl : public interface
class bps_chan_ctrl : public itface
{
public:
using ptr = std::shared_ptr<bps_chan_ctrl>;
Expand Down
2 changes: 1 addition & 1 deletion source/pza/interfaces/meter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using namespace pza;

meter::meter(device *device, const std::string &name)
: interface(device, name),
: itface(device, name),
_measure("measure")
{
_measure.add_ro_field<double>("value");
Expand Down
2 changes: 1 addition & 1 deletion source/pza/interfaces/meter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <pza/core/interface.hxx>

namespace pza {
class meter : public interface
class meter : public itface
{
public:
meter(device *device, const std::string &name);
Expand Down

0 comments on commit 4daf993

Please sign in to comment.