Fix the build on MacOS

Three things kept it from building there:

- librt was linked unconditionally, MacOS has those functions in its system
  library and no librt to link
- -march=native and -mtune=native are not accepted by the compilers on Apple
  Silicon. They are dropped there, the speed of the training is not what MacOS
  is used for here
- homebrew installs into /opt/homebrew on Apple Silicon, which was in neither
  the include nor the library path

STATIC_DEREKO2VEC stops with an explanation on MacOS now instead of failing at
link time, as there are no static system libraries to link against.

Verified on Linux that nothing changes: -march=native and -lrt are still used,
the build and the training test pass. The MacOS paths themselves were only
exercised by forcing APPLE, a real build there has not been tried.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Change-Id: I60846a306241901308282c4ae1a3e5b67649d51e
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 32e1d1e..4076589 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,18 +1,35 @@
 cmake_minimum_required(VERSION 3.9)
 project(dereko2vec VERSION 0.9.1 DESCRIPTION "Fork from wang2vec with extensions for re-training and count based models")
 option(STATIC_DEREKO2VEC "Build dereko2vec as a static binary" OFF)
+if (STATIC_DEREKO2VEC AND APPLE)
+    message(FATAL_ERROR "STATIC_DEREKO2VEC does not work on MacOS, which ships "
+            "no static system libraries. Build without it.")
+endif ()
 
 set(CMAKE_VERBOSE_MAKEFILE on)
+
+if (APPLE)
+    # No librt, its functions are in the system library. -march=native is not
+    # accepted by the compilers on Apple Silicon, and the speed of the training
+    # is not what MacOS is used for here.
+    set(LIBRT "")
+    set(TUNE_FLAGS "")
+else ()
+    set(LIBRT "rt")
+    set(TUNE_FLAGS "-march=native -mtune=native")
+endif ()
+
 if (CMAKE_BUILD_TYPE MATCHES Debug)
     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -Wall -Wextra -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-but-set-variable -Wno-unused-but-set-parameter -Wno-unused-value -Wno-unused-result")
 else ()
-    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -march=native -mtune=native -Wall -funroll-loops")
+    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 ${TUNE_FLAGS} -Wall -funroll-loops")
 endif ()
-set(ADDITIONAL_LIBS ${ADDITIONAL_LIBS} m pthread rt snappy z bz2 lz4 zstd stdc++ dl)
+set(ADDITIONAL_LIBS ${ADDITIONAL_LIBS} m pthread ${LIBRT} snappy z bz2 lz4 zstd stdc++ dl)
 include(GNUInstallDirs)
 
-include_directories(/usr/local/include;/usr/local/kl/include)
-link_directories(/usr/local/kl/lib;/usr/local/lib;/usr/local/lib64)
+# /opt/homebrew is where homebrew installs on Apple Silicon
+include_directories(/usr/local/include /usr/local/kl/include /opt/homebrew/include)
+link_directories(/usr/local/kl/lib /usr/local/lib /usr/local/lib64 /opt/homebrew/lib)
 
 # A static dereko2vec needs everything as a static library. An ordinary build
 # takes the shared ones, and it has to take them explicitly: a leftover static