Skip to content

Commit

Permalink
doxy: add a section about building with GNU Make and CMake
Browse files Browse the repository at this point in the history
The GNU Make stuff is moved from the API example,
and CMake is added thanks to Florent Pruvost's example at
https://gitlab.inria.fr/solverstack/distrib/-/tree/master/cmake/test/hwloc

Refs #565

Signed-off-by: Brice Goglin <Brice.Goglin@inria.fr>
(cherry picked from commit cd3a1a7)
  • Loading branch information
bgoglin committed Dec 20, 2023
1 parent 30aa5e5 commit 4f9fe46
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 19 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Version 2.10.1
* Add bandwidths between subdevices in the LevelZero XeLinkBandwidth
matrix.
* Fix CUDA/OpenCL test source path in Windows CMake.
* Add section "Compiling software on top of hwloc's C API" in the
documentation with examples for GNU Make and CMake,
thanks to Florent Pruvost for the help.


Version 2.10.0
Expand Down
10 changes: 2 additions & 8 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -418,14 +418,8 @@ return 0;
}

hwloc provides a pkg-config executable to obtain relevant compiler and linker
flags. For example, it can be used thusly to compile applications that utilize
the hwloc library (assuming GNU Make):

CFLAGS += $(shell pkg-config --cflags hwloc)
LDLIBS += $(shell pkg-config --libs hwloc)

hwloc-hello: hwloc-hello.c
$(CC) hwloc-hello.c $(CFLAGS) -o hwloc-hello $(LDLIBS)
flags. See Compiling software on top of hwloc's C API for details on building
program on top of hwloc's API using GNU Make or CMake.

On a machine 2 processor packages -- each package of which has two processing
cores -- the output from running hwloc-hello could be something like the
Expand Down
84 changes: 73 additions & 11 deletions doc/hwloc.doxy
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<li> Chapters
<ul>
<li> \ref installation
<li> \ref useapi
<li> \ref termsanddefs
<li> \ref tools
<li> \ref envvar
Expand Down Expand Up @@ -365,16 +366,8 @@ tree.
\include examples/hwloc-hello.c

hwloc provides a \c pkg-config executable to obtain relevant compiler
and linker flags. For example, it can be used thusly to compile
applications that utilize the hwloc library (assuming GNU Make):

\verbatim
CFLAGS += $(shell pkg-config --cflags hwloc)
LDLIBS += $(shell pkg-config --libs hwloc)

hwloc-hello: hwloc-hello.c
$(CC) hwloc-hello.c $(CFLAGS) -o hwloc-hello $(LDLIBS)
\endverbatim
and linker flags. See \ref useapi for details on building program
on top of hwloc's API using GNU Make or CMake.

On a machine 2 processor packages -- each package of
which has two processing cores -- the output from running \c
Expand Down Expand Up @@ -590,9 +583,78 @@ or GNU Autotools.




\page useapi Compiling software on top of hwloc's C API

A program using the hwloc C API (for instance with <tt>hwloc-hello.c</tt>
presented in \ref interface_example) may be built with standard
development tools.
<tt>pkg-config</tt> provides easy ways to retrieve the required compiler
and linker flags as described below, but it is not mandatory.


\section useapi_gnumake Compiling on top of hwloc's C API with GNU Make

Here's an example of Makefile for building <tt>hwloc-hello.c</tt>
with GNU Make:

\verbatim
CFLAGS += $(shell pkg-config --cflags hwloc)
LDLIBS += $(shell pkg-config --libs hwloc)

hwloc-hello: hwloc-hello.c
$(CC) hwloc-hello.c $(CFLAGS) -o hwloc-hello $(LDLIBS)
\endverbatim


\section useapi_cmake Compiling on top of hwloc's C API with CMake

Here's an example de <tt>CMakeLists.txt</tt> which shows variables
obtained from <tt>pkg-config</tt> and how to use them:

\verbatim
cmake_minimum_required(VERSION 3.5)
project(TEST_HWLOC C)
include(FindPkgConfig)
if(PKG_CONFIG_EXECUTABLE)
unset(HWLOC_FOUND CACHE)
pkg_search_module(HWLOC hwloc)
if(HWLOC_FOUND)
message(STATUS "HWLOC_LIBRARIES=${HWLOC_LIBRARIES}")
message(STATUS "HWLOC_LINK_LIBRARIES=${HWLOC_LINK_LIBRARIES}")
message(STATUS "HWLOC_LIBRARY_DIRS=${HWLOC_LIBRARY_DIRS}")
message(STATUS "HWLOC_LDFLAGS=${HWLOC_LDFLAGS}")
message(STATUS "HWLOC_LDFLAGS_OTHERS=${HWLOC_LDFLAGS_OTHERS}")
message(STATUS "HWLOC_INCLUDE_DIRS=${HWLOC_INCLUDE_DIRS}")
message(STATUS "HWLOC_CFLAGS=${HWLOC_CFLAGS}")
message(STATUS "HWLOC_CFLAGS_OTHER=${HWLOC_CFLAGS_OTHER}")
else()
message(FATAL_ERROR "HWLOC not found with pkg-config, add the path to hwloc.pc in PKG_CONFIG_PATH.")
endif()
else()
message(FATAL_ERROR "PKG_CONFIG_EXECUTABLE: not found.")
endif()

add_executable(hwloc-hello hwloc-hello.c)
target_include_directories(hwloc-hello PRIVATE ${HWLOC_INCLUDE_DIRS})
target_compile_options(hwloc-hello PRIVATE ${HWLOC_CFLAGS})
target_link_libraries(hwloc-hello PRIVATE ${HWLOC_LINK_LIBRARIES})
target_link_options(hwloc-hello PRIVATE ${HWLOC_LDFLAGS})
\endverbatim

The project may be built with:
\verbatim
cmake -B build
cmake --build build --verbose
\endverbatim

The built binary is then available under <tt>build/hwloc-hello</tt>.




\page termsanddefs Terms and Definitions


\htmlonly
<div class="section" id="termsanddefs_objects">
\endhtmlonly
Expand Down

0 comments on commit 4f9fe46

Please sign in to comment.