Files
imhex/cmake/modules/FindMagic.cmake
Alex ea9b197d36 build: improve FindMagic.cmake module (#2610)
Now it properly fails if libmagic is not installed in the system.

The error message would look like:

```
CMake Error at cmake/modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Magic (missing: LIBMAGIC_LIBRARY LIBMAGIC_INCLUDE_DIR)
  (Required is at least version "5.39")
Call Stack (most recent call first):
  cmake/modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
  cmake/modules/FindMagic.cmake:5 (find_package_handle_standard_args)
  cmake/build_helpers.cmake:971 (find_package)
  CMakeLists.txt:81 (addBundledLibraries)
```

### Problem description

The current `FindMagic.cmake` module doesn't properly report failure
about absent libmagic libs and #includes. This behavior leads to the
compile errors on the build stage (possibly after some time, which will
be simply wasted). This Find module should set the `Magic_FOUND`
variable and handle all required variables( LIBMAGIC_INCLUDE_DIR and
LIBMAGIC_LIBRARY) to present.
But the current module sets the `libmagic_FOUND` variable, so the CMake
assumes that it is found something. This behaviour should also be
handled by `find_package_handle_standard_args()` ("FPHSA"), and it is
actually handled, but as we requested the `Magic` package to be
required, the former function ("FPHSA") didn't stop the configure
process.

### Implementation description
 - Clean up the unused ifs, as "FPHSA" already done it better.
- Use the proper name for "FPHSA" so it could set the proper
`Magic_FOUND` and could make sure that the requested package is
`REQUIRED`.

### Screenshots
Before:
```
-- Could NOT find libmagic (missing: LIBMAGIC_LIBRARY LIBMAGIC_INCLUDE_DIR)
```

<img width="1005" height="24" alt="изображение"
src="https://github.com/user-attachments/assets/082e2842-ff94-4418-8f86-86021c7dd23d"
/>

After:

```
CMake Error at cmake/modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Magic (missing: LIBMAGIC_LIBRARY LIBMAGIC_INCLUDE_DIR)
  (Required is at least version "5.39")
Call Stack (most recent call first):
  cmake/modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
  cmake/modules/FindMagic.cmake:5 (find_package_handle_standard_args)
  cmake/build_helpers.cmake:971 (find_package)
  CMakeLists.txt:81 (addBundledLibraries)
```

### Additional things

Closes #2244 .
2026-01-07 17:13:41 +01:00

15 lines
288 B
CMake

find_path(LIBMAGIC_INCLUDE_DIR magic.h)
find_library(LIBMAGIC_LIBRARY NAMES magic)
find_package_handle_standard_args(Magic DEFAULT_MSG
LIBMAGIC_LIBRARY
LIBMAGIC_INCLUDE_DIR
)
mark_as_advanced(
LIBMAGIC_INCLUDE_DIR
LIBMAGIC_LIBRARY
Magic_FOUND
)