Skip to content

Commit efa52fb

Browse files
author
MarkusB
committed
fix: prev commit
1 parent 30fab5c commit efa52fb

2 files changed

Lines changed: 45 additions & 27 deletions

File tree

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@ mb_pre_commit_setup()
5050
mb_pre_commit_setup_subdirectory(PRE_COMMIT_INSTALL_EXAMPLE_CONFIG OFF)
5151
```
5252

53-
That installs hooks and a sweep target named `mb-pre-commit-sweep-<id>` where `<id>` is
54-
this tree's `project()` name when it differs from the top-level project, otherwise the
55-
source directory name (e.g. `mb-pre-commit-sweep-devenv`) so it does not clash with the
56-
parent's `mb-pre-commit-sweep`.
53+
That installs hooks and a sweep target named `mb-pre-commit-sweep-<dir>` where `<dir>` is
54+
the basename of the directory containing the calling `CMakeLists.txt` (e.g.
55+
`mb-pre-commit-sweep-devenv`) so it does not clash with the parent's `mb-pre-commit-sweep`.
5756

5857
Or without CMake:
5958

@@ -193,9 +192,9 @@ This project’s CMake module uses APIs that require **CMake 3.21+** (`file(COPY
193192
Same options as `mb_pre_commit_setup`, but **`PROJECT_SOURCE_DIR`** / **`PROJECT_BINARY_DIR`** default to
194193
**`CMAKE_CURRENT_SOURCE_DIR`** / **`CMAKE_CURRENT_BINARY_DIR`** (the project whose `CMakeLists.txt`
195194
calls it), and **`PRE_COMMIT_INSTALL_EXAMPLE_CONFIG`** defaults to **`OFF`** unless you set it.
196-
**`PRE_COMMIT_SWEEP_TARGET`** defaults to `mb-pre-commit-sweep-<id>`: this tree's
197-
`PROJECT_NAME` when it is not the top-level `CMAKE_PROJECT_NAME`, otherwise the basename of
198-
`CMAKE_CURRENT_SOURCE_DIR`.
195+
**`PRE_COMMIT_SWEEP_TARGET`** defaults to `mb-pre-commit-sweep-<dir>` where `<dir>` is the
196+
basename of `CMAKE_CURRENT_LIST_DIR` at the call site (the folder that contains the calling
197+
`CMakeLists.txt`).
199198

200199
Relative paths for `PROJECT_SOURCE_DIR` / `PROJECT_BINARY_DIR` / `PRE_COMMIT_VENV_DIR` are resolved against
201200
`CMAKE_SOURCE_DIR`, `CMAKE_BINARY_DIR`, and `PROJECT_SOURCE_DIR` respectively, matching CMake’s usual behavior.

cmake/mb-pre-commit.cmake

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -266,37 +266,42 @@ endfunction()
266266
# Uses CMAKE_CURRENT_SOURCE_DIR/BINARY_DIR instead of the top-level CMAKE_SOURCE_DIR so Git
267267
# hooks and .venv land in the submodule tree. Commits inside that submodule then run this
268268
# hook; the parent's mb_pre_commit_setup() does not apply there.
269-
function(mb_pre_commit_setup_subdirectory)
270-
cmake_parse_arguments(
271-
SUBPC
272-
""
273-
"PRE_COMMIT_SWEEP_TARGET;PRE_COMMIT_INSTALL_EXAMPLE_CONFIG"
274-
""
275-
${ARGN}
269+
function(_mb_pre_commit_setup_subdirectory_impl)
270+
set(options)
271+
set(oneValueArgs
272+
CALLER_LIST_DIR
273+
CALLER_SOURCE_DIR
274+
CALLER_BINARY_DIR
275+
PRE_COMMIT_SWEEP_TARGET
276+
PRE_COMMIT_INSTALL_EXAMPLE_CONFIG
276277
)
278+
cmake_parse_arguments(SUBPC "${options}" "${oneValueArgs}" "" ${ARGN})
279+
280+
if(
281+
NOT SUBPC_CALLER_LIST_DIR
282+
OR NOT SUBPC_CALLER_SOURCE_DIR
283+
OR NOT SUBPC_CALLER_BINARY_DIR
284+
)
285+
message(
286+
FATAL_ERROR
287+
"_mb_pre_commit_setup_subdirectory_impl: internal caller paths missing"
288+
)
289+
endif()
277290

278291
if(NOT SUBPC_PRE_COMMIT_SWEEP_TARGET)
279-
# CMAKE_PROJECT_NAME is always the top-level project(); nested add_subdirectory
280-
# trees need PROJECT_NAME or the source dir leaf so targets do not collide.
281-
if(PROJECT_NAME AND NOT PROJECT_NAME STREQUAL CMAKE_PROJECT_NAME)
282-
set(_subpc_sweep_id "${PROJECT_NAME}")
283-
else()
284-
get_filename_component(
285-
_subpc_sweep_id
286-
"${CMAKE_CURRENT_SOURCE_DIR}"
287-
NAME
288-
)
289-
endif()
292+
# Do not use PROJECT_NAME or CMAKE_PROJECT_NAME: in a function they may not reflect
293+
# the caller's directory scope (PROJECT_NAME inherits the top-level project() name).
294+
get_filename_component(_subpc_sweep_id "${SUBPC_CALLER_LIST_DIR}" NAME)
290295
set(SUBPC_PRE_COMMIT_SWEEP_TARGET
291296
"mb-pre-commit-sweep-${_subpc_sweep_id}"
292297
)
293298
endif()
294299

295300
set(_sub_setup_args
296301
PROJECT_SOURCE_DIR
297-
"${CMAKE_CURRENT_SOURCE_DIR}"
302+
"${SUBPC_CALLER_SOURCE_DIR}"
298303
PROJECT_BINARY_DIR
299-
"${CMAKE_CURRENT_BINARY_DIR}"
304+
"${SUBPC_CALLER_BINARY_DIR}"
300305
PRE_COMMIT_SWEEP_TARGET
301306
"${SUBPC_PRE_COMMIT_SWEEP_TARGET}"
302307
)
@@ -316,3 +321,17 @@ function(mb_pre_commit_setup_subdirectory)
316321

317322
mb_pre_commit_setup(${_sub_setup_args})
318323
endfunction()
324+
325+
# Macro so CMAKE_CURRENT_* are read at the call site (the submodule CMakeLists.txt), not
326+
# inside a function scope where PROJECT_NAME can still be the top-level project() name.
327+
macro(mb_pre_commit_setup_subdirectory)
328+
_mb_pre_commit_setup_subdirectory_impl(
329+
CALLER_LIST_DIR
330+
"${CMAKE_CURRENT_LIST_DIR}"
331+
CALLER_SOURCE_DIR
332+
"${CMAKE_CURRENT_SOURCE_DIR}"
333+
CALLER_BINARY_DIR
334+
"${CMAKE_CURRENT_BINARY_DIR}"
335+
${ARGN}
336+
)
337+
endmacro()

0 commit comments

Comments
 (0)