Skip to content

Commit f0c8ca7

Browse files
Support compiler MSYS2-MinGW (#5600) (#5633)
* feature: support compiler MSYS2-MinGW #5575 Signed-off-by: RookieCLY <2238379153@qq.com> * feature: support compiler MSYS2-MinGW #5575 Signed-off-by: RookieCLY <2238379153@qq.com> * feature: support compiler MSYS2-MinGW and fix some format problem #5575 Signed-off-by: RookieCLY <2238379153@qq.com> --------- Signed-off-by: RookieCLY <2238379153@qq.com> (cherry picked from commit 28b11ca) Co-authored-by: RookieCLY <2238379153@qq.com>
1 parent 5c8457d commit f0c8ca7

File tree

9 files changed

+240
-12
lines changed

9 files changed

+240
-12
lines changed

CMakeLists.txt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,26 @@ endif()
395395
###############################################################################
396396
# Tools default setup
397397
###############################################################################
398-
option(COMPILE_TOOLS "Build tools" ON)
398+
if(WIN32 AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
399+
execute_process(
400+
COMMAND ${CMAKE_CXX_COMPILER} -dumpmachine
401+
OUTPUT_VARIABLE COMPILER_MACHINE
402+
OUTPUT_STRIP_TRAILING_WHITESPACE
403+
)
404+
if(COMPILER_MACHINE MATCHES "mingw")
405+
message(STATUS "Using MinGW compiler.")
406+
option(COMPILE_TOOLS "Build tools" OFF)
407+
add_definitions(-DMINGW_COMPILER=1)
408+
set(CMAKE_CXX_FLAGS
409+
"${CMAKE_CXX_FLAGS} -Wno-attributes -Wno-stringop-overread -Wno-builtin-macro-redefined -Wno-cast-function-type -Wno-unused-variable -Wno-unused-parameter -Wno-unused-function -Wno-missing-field-initializers")
410+
target_link_libraries(fastdds PUBLIC ws2_32 mswsock)
411+
else()
412+
message(STATUS "Using a GNU compiler on Windows but not MinGW.")
413+
endif()
414+
else()
415+
message(STATUS "Not using a GNU compiler on Windows.")
416+
option(COMPILE_TOOLS "Build tools" ON)
417+
endif()
399418

400419
if(EPROSIMA_BUILD)
401420
set(COMPILE_TOOLS ON)

include/fastdds/fastdds_dll.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,19 @@
4444

4545
#if defined(_WIN32)
4646
#if defined(EPROSIMA_ALL_DYN_LINK) || defined(FASTDDS_DYN_LINK)
47-
#if defined(fastdds_EXPORTS)
48-
#define FASTDDS_EXPORTED_API __declspec( dllexport )
47+
#if defined(MINGW_COMPILER)
48+
#if defined(fastdds_EXPORTS)
49+
#define FASTDDS_EXPORTED_API __declspec( dllexport )
50+
#else
51+
#define FASTDDS_EXPORTED_API __attribute__((visibility("default")))
52+
#endif // FASTDDS_SOURCE
4953
#else
50-
#define FASTDDS_EXPORTED_API __declspec( dllimport )
51-
#endif // FASTDDS_SOURCE
54+
#if defined(fastdds_EXPORTS)
55+
#define FASTDDS_EXPORTED_API __declspec( dllexport )
56+
#else
57+
#define FASTDDS_EXPORTED_API __declspec( dllimport )
58+
#endif // FASTDDS_SOURCE
59+
#endif // if defined(MINGW_COMPILER)
5260
#else
5361
#define FASTDDS_EXPORTED_API
5462
#endif // if defined(EPROSIMA_ALL_DYN_LINK) || defined(FASTDDS_DYN_LINK)

include/fastdds/utils/TimedMutex.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#if defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 193632528
2828
#include <mutex>
29+
#elif defined(MINGW_COMPILER)
30+
#include <mutex>
2931
#else
3032
#include <thread>
3133
extern int clock_gettime(
@@ -47,6 +49,9 @@ namespace fastdds {
4749
#if defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 193632528
4850
using TimedMutex = std::timed_mutex;
4951
using RecursiveTimedMutex = std::recursive_timed_mutex;
52+
#elif defined(MINGW_COMPILER)
53+
using TimedMutex = std::timed_mutex;
54+
using RecursiveTimedMutex = std::recursive_timed_mutex;
5055
#else
5156
class TimedMutex
5257
{

src/cpp/fastdds/xtypes/serializers/json/dynamic_data_json.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
// limitations under the License.
1414

1515
#include <bitset>
16-
#include <codecvt>
16+
#ifndef MINGW_COMPILER
17+
#include <codecvt>
18+
#endif // ifndef MINGW_COMPILER
1719
#include <iomanip>
1820
#include <iostream>
1921
#include <string>
@@ -734,9 +736,21 @@ ReturnCode_t json_serialize_basic_member(
734736
if (RETCODE_OK == ret)
735737
{
736738
// Insert UTF-8 converted value
739+
#if defined(MINGW_COMPILER)
740+
std::wstring aux_wstring_value({value});
741+
std::string utf8_value;
742+
int size_needed = std::wcstombs(nullptr, aux_wstring_value.data(), 0);
743+
if (size_needed > 0)
744+
{
745+
utf8_value.resize(size_needed);
746+
std::wcstombs(&utf8_value[0], aux_wstring_value.data(), size_needed);
747+
}
748+
#else
737749
std::wstring aux_wstring_value({value});
738750
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
739751
std::string utf8_value = converter.to_bytes(aux_wstring_value);
752+
753+
#endif // defined(MINGW_COMPILER)
740754
json_insert(member_name, utf8_value, output);
741755
}
742756
else
@@ -766,8 +780,18 @@ ReturnCode_t json_serialize_basic_member(
766780
if (RETCODE_OK == ret)
767781
{
768782
// Insert UTF-8 converted value
783+
#ifdef MINGW_COMPILER
784+
std::string utf8_value;
785+
int size_needed = std::wcstombs(nullptr, value.data(), 0);
786+
if (size_needed > 0)
787+
{
788+
utf8_value.resize(size_needed);
789+
std::wcstombs(&utf8_value[0], value.data(), size_needed);
790+
}
791+
#else
769792
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
770793
std::string utf8_value = converter.to_bytes(value);
794+
#endif // defined(MINGW_COMPILER)
771795
json_insert(member_name, utf8_value, output);
772796
}
773797
else

src/cpp/utils/SystemInfo.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,18 @@ fastdds::dds::ReturnCode_t SystemInfo::get_username(
155155
bool SystemInfo::file_exists(
156156
const std::string& filename)
157157
{
158+
#ifdef _WIN32
159+
// modify for mingw
160+
DWORD fileAttributes = GetFileAttributesA(filename.c_str());
161+
if (fileAttributes == INVALID_FILE_ATTRIBUTES)
162+
{
163+
return false;
164+
}
165+
return !(fileAttributes & FILE_ATTRIBUTE_DIRECTORY);
166+
#else
158167
struct stat s;
159-
// Check existence and that it is a regular file (and not a folder)
160168
return (stat(filename.c_str(), &s) == 0 && s.st_mode & S_IFREG);
169+
#endif // ifdef _WIN32
161170
}
162171

163172
bool SystemInfo::wait_for_file_closure(

src/cpp/utils/TimedConditionVariable.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@
5050
return 0;
5151
}
5252
*/
53-
#define exp7 10000000i64 //1E+7 //C-file part
54-
#define exp9 1000000000i64 //1E+9
55-
#define w2ux 116444736000000000i64 //1.jan1601 to 1.jan1970
53+
#ifdef MINGW_COMPILER
54+
#define exp7 10000000LL //1E+7 //C-file part
55+
#define exp9 1000000000LL //1E+9
56+
#define w2ux 116444736000000000LL //1.jan1601 to 1.jan1970
57+
#else
58+
#define exp7 10000000i64 //1E+7 //C-file part
59+
#define exp9 1000000000i64 //1E+9
60+
#define w2ux 116444736000000000i64 //1.jan1601 to 1.jan1970
61+
#endif // ifdef MINGW_COMPILER
5662
void unix_time(
5763
struct timespec* spec)
5864
{

src/cpp/utils/shared_memory/RobustExclusiveLock.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#ifdef _MSC_VER
1919
#include <io.h>
20+
#elif defined(MINGW_COMPILER)
21+
#include <io.h>
2022
#else
2123
#include <sys/file.h>
2224
#endif // ifdef _MSC_VER
@@ -165,6 +167,45 @@ class RobustExclusiveLock
165167
}
166168
}
167169

170+
#elif defined(MINGW_COMPILER)
171+
static int open_and_lock_file(
172+
const std::string& file_path,
173+
bool* was_lock_created)
174+
{
175+
int test_exist;
176+
auto ret = _sopen_s(&test_exist, file_path.c_str(), O_RDONLY, 0x0010, _S_IREAD | _S_IWRITE);
177+
178+
if (ret == 0)
179+
{
180+
*was_lock_created = false;
181+
return test_exist;
182+
}
183+
184+
int fd;
185+
ret = _sopen_s(&fd, file_path.c_str(), O_CREAT | O_RDONLY, 0x0010, _S_IREAD | _S_IWRITE);
186+
187+
if (ret != 0)
188+
{
189+
return -1;
190+
}
191+
192+
*was_lock_created = true;
193+
194+
return fd;
195+
}
196+
197+
static void unlock_and_close(
198+
int fd,
199+
const std::string& name)
200+
{
201+
_close(fd);
202+
203+
if (0 != std::remove(SharedDir::get_lock_path(name).c_str()))
204+
{
205+
EPROSIMA_LOG_WARNING(RTPS_TRANSPORT_SHM, "Failed to remove " << SharedDir::get_lock_path(name));
206+
}
207+
}
208+
168209
#else
169210

170211
static int open_and_lock_file(

src/cpp/utils/shared_memory/RobustSharedLock.hpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#ifdef _MSC_VER
1919
#include <io.h>
20+
#elif defined(MINGW_COMPILER)
21+
#include <io.h>
2022
#else
2123
#include <sys/file.h>
2224
#endif // ifdef _MSC_VER
@@ -230,6 +232,118 @@ class RobustSharedLock
230232
return lock_status;
231233
}
232234

235+
#elif defined(MINGW_COMPILER)
236+
int open_and_lock_file(
237+
const std::string& file_path,
238+
bool* was_lock_created,
239+
bool* was_lock_released)
240+
{
241+
int test_exist;
242+
243+
// Try open exclusive
244+
auto ret = _sopen_s(&test_exist, file_path.c_str(), _O_WRONLY, 0x0010, _S_IREAD | _S_IWRITE);
245+
246+
if (ret == 0)
247+
{
248+
*was_lock_created = false;
249+
250+
if (was_lock_released)
251+
{
252+
*was_lock_released = true;
253+
}
254+
255+
_close(test_exist);
256+
}
257+
else
258+
{
259+
// Try open shared
260+
ret = _sopen_s(&test_exist, file_path.c_str(), _O_RDONLY, 0x0010, _S_IREAD | _S_IWRITE);
261+
262+
if (ret == 0)
263+
{
264+
if (was_lock_released)
265+
{
266+
*was_lock_released = false;
267+
}
268+
269+
*was_lock_created = false;
270+
271+
return test_exist;
272+
}
273+
else
274+
{
275+
if (was_lock_released)
276+
{
277+
*was_lock_released = true;
278+
}
279+
280+
*was_lock_created = true;
281+
}
282+
}
283+
284+
int fd;
285+
// Open or create shared
286+
ret = _sopen_s(&fd, file_path.c_str(), O_CREAT | _O_RDONLY, 0x0010, _S_IREAD | _S_IWRITE);
287+
288+
if (ret != 0)
289+
{
290+
char errmsg[1024];
291+
strerror_s(errmsg, sizeof(errmsg), errno);
292+
throw std::runtime_error("failed to open/create " + file_path + " " + std::string(errmsg));
293+
}
294+
295+
return fd;
296+
}
297+
298+
void unlock_and_close()
299+
{
300+
_close(fd_);
301+
302+
test_lock(SharedDir::get_lock_path(name_), true);
303+
}
304+
305+
static LockStatus test_lock(
306+
const std::string& file_path,
307+
bool remove_if_unlocked = false)
308+
{
309+
LockStatus lock_status;
310+
311+
int fd;
312+
auto ret = _sopen_s(&fd, file_path.c_str(), _O_RDONLY, 0x0010, _S_IREAD | _S_IWRITE);
313+
314+
if (ret == 0)
315+
{
316+
lock_status = LockStatus::NOT_LOCKED;
317+
318+
_close(fd);
319+
320+
// Lock exclusive
321+
ret = _sopen_s(&fd, file_path.c_str(), _O_WRONLY, 0x0010, _S_IREAD | _S_IWRITE);
322+
if (ret != 0)
323+
{
324+
lock_status = LockStatus::LOCKED;
325+
}
326+
else
327+
{
328+
_close(fd);
329+
}
330+
}
331+
else
332+
{
333+
lock_status = LockStatus::OPEN_FAILED;
334+
}
335+
336+
if (lock_status == LockStatus::NOT_LOCKED && remove_if_unlocked)
337+
{
338+
if (0 != std::remove(file_path.c_str()))
339+
{
340+
EPROSIMA_LOG_WARNING(RTPS_TRANSPORT_SHM, "Failed to remove " << file_path);
341+
}
342+
}
343+
344+
return lock_status;
345+
}
346+
233347
#else
234348

235349
int open_and_lock_file(

thirdparty/filewatch/FileWatch.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030

3131
#ifdef _WIN32
3232
#define WIN32_LEAN_AND_MEAN
33-
#define stat _stat
33+
#ifndef MINGW_COMPILER
34+
#define stat _stat // do not use stat in windows
35+
#endif // ifndef MINGW_COMPILER
3436
#ifndef NOMINMAX
3537
#define NOMINMAX
3638
#endif
@@ -404,7 +406,7 @@ namespace filewatch {
404406
std::ratio_multiply<std::hecto, typename std::chrono::nanoseconds::period>>(
405407
reinterpret_cast<ULARGE_INTEGER*>(&att.ftLastWriteTime)->QuadPart - base_.first.QuadPart);
406408

407-
if (bytes_returned == 0 || (current_time == last_write_time_) && current_size == last_size_ ) {
409+
if (bytes_returned == 0 || ((current_time == last_write_time_) && current_size == last_size_ )) {
408410
break;
409411
}
410412

0 commit comments

Comments
 (0)