Skip to content

Commit e3498e7

Browse files
authored
Fix std::filesystem linking (#295) (#301)
(cherry picked from commit 39b15b0) Signed-off-by: zichguan-amd <zichuan.guan@amd.com>
1 parent b8d1e8d commit e3498e7

File tree

3 files changed

+80
-9
lines changed

3 files changed

+80
-9
lines changed

Common/FindFilesystem.cmake

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
24+
25+
include(CMakePushCheckState)
26+
include(CheckCXXSourceCompiles)
27+
28+
set(CMAKE_CXX_STANDARD 17)
29+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
30+
31+
cmake_push_check_state()
32+
33+
set(test_code
34+
[[
35+
#include <filesystem>
36+
#include <iostream>
37+
38+
namespace fs = std::filesystem;
39+
40+
int main()
41+
{
42+
std::cout << fs::current_path() << std::endl;
43+
return 0;
44+
}
45+
]]
46+
)
47+
# Check if std::filesystem works without additional libraries
48+
check_cxx_source_compiles("${test_code}" CXX_FS_NO_LINK)
49+
50+
if (CXX_FS_NO_LINK)
51+
message(STATUS "No extra linking required to use std::filesystem")
52+
else()
53+
# Check if we can link stdc++fs
54+
set(CMAKE_REQUIRED_LIBRARIES stdc++fs)
55+
check_cxx_source_compiles("${test_code}" CXX_FS_CAN_LINK)
56+
if (CXX_FS_CAN_LINK)
57+
set(CXX_FS_LIBRARY stdc++fs CACHE STRING "Additional library required to use std::filesystem" FORCE)
58+
message(STATUS "Need explicite linking to stdc++fs")
59+
endif()
60+
endif()
61+
62+
unset(test_code)
63+
cmake_pop_check_state()
64+
65+
if(NOT CXX_FS_NO_LINK AND NOT CXX_FS_CAN_LINK)
66+
message(FATAL_ERROR "Cannot run simple program using std::filesystem")
67+
endif()

HIP-Basic/module_api/CMakeLists.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ set(example_name hip_module_api)
2525
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
2626
project(${example_name} LANGUAGES CXX)
2727

28+
include("${CMAKE_CURRENT_LIST_DIR}/../../Common/FindFilesystem.cmake")
2829
include("${CMAKE_CURRENT_LIST_DIR}/../../Common/HipPlatform.cmake")
2930
select_gpu_language()
3031

@@ -83,9 +84,7 @@ add_test(NAME ${example_name} COMMAND ${example_name})
8384

8485
set(include_dirs "../../Common" "../../External")
8586
target_include_directories(${example_name} PRIVATE ${include_dirs})
86-
set_source_files_properties(
87-
main.hip
88-
PROPERTIES LANGUAGE ${ROCM_EXAMPLES_GPU_LANGUAGE}
89-
)
87+
target_link_libraries(${example_name} PRIVATE ${CXX_FS_LIBRARY})
88+
set_source_files_properties(main.hip PROPERTIES LANGUAGE ${ROCM_EXAMPLES_GPU_LANGUAGE})
9089

9190
install(TARGETS ${example_name})

HIP-Basic/module_api/Makefile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MIT License
22
#
3-
# Copyright (c) 2024 Advanced Micro Devices, Inc. All rights reserved.
3+
# Copyright (c) 2024-2025 Advanced Micro Devices, Inc. All rights reserved.
44
#
55
# Permission is hereby granted, free of charge, to any person obtaining a copy
66
# of this software and associated documentation files (the "Software"), to deal
@@ -37,10 +37,15 @@ HIPCXX ?= $(ROCM_INSTALL_DIR)/bin/hipcc
3737
# Common variables and flags
3838
CXX_STD := c++17
3939
CXXFLAGS ?= -Wall -Wextra
40-
ICXXFLAGS := -std=$(CXX_STD) $(CXXFLAGS)
41-
ICPPFLAGS := -I $(COMMON_INCLUDE_DIR) $(CPPFLAGS)
42-
ILDFLAGS := $(LDFLAGS)
43-
ILDLIBS := $(LDLIBS)
40+
ICXXFLAGS := -std=$(CXX_STD)
41+
ICPPFLAGS := -I $(COMMON_INCLUDE_DIR)
42+
ILDFLAGS :=
43+
ILDLIBS := -lstdc++fs
44+
45+
ICXXFLAGS += $(CXXFLAGS)
46+
ICPPFLAGS += $(CPPFLAGS)
47+
ILDFLAGS += $(LDFLAGS)
48+
ILDLIBS += $(LDLIBS)
4449

4550
$(EXAMPLE): main.hip $(COMMON_INCLUDE_DIR)/example_utils.hpp module.co
4651
$(HIPCXX) $(ICXXFLAGS) $(ICPPFLAGS) $(ILDFLAGS) -o $@ $< $(ILDLIBS)

0 commit comments

Comments
 (0)