From 7d61173d5c416f707804c7ffad6b2aaea32f3a3d Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Mon, 1 Nov 2021 11:59:42 +1000
Subject: Fix build for Haiku


diff --git a/CMakeLists.txt b/CMakeLists.txt
index aada9ad..4d14564 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -312,6 +312,19 @@ if (ANDROID)
 endif ()
 
 # Add security (hardening flags)
+IF (HAIKU)
+	SET(SEC_COMP_FLAGS "-D_FORTIFY_SOURCE=2 -Wcast-align -Wa,--noexecstack")
+	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SEC_COMP_FLAGS}")
+	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SEC_COMP_FLAGS}")
+	SET(SEC_LINK_FLAGS "-Wl,-z,nodump -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now")
+	IF("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
+		MESSAGE("++ 64 bit architecture")
+		SET(SEC_LINK_FLAGS "${SEC_LINK_FLAGS} -pie")
+		SET(SEC_COMP_FLAGS "${SEC_COMP_FLAGS} -fPIE")
+	ENDIF()
+	SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${SEC_LINK_FLAGS}")
+	SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${SEC_LINK_FLAGS}")
+ELSE ()
 IF (UNIX OR APPLE OR ANDROID)
     SET(SEC_COMP_FLAGS "-D_FORTIFY_SOURCE=2 -fstack-protector-all -Wcast-align -fPIE")
     IF (NOT ANDROID AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND NOT APPLE)
@@ -333,6 +346,7 @@ IF (UNIX OR APPLE OR ANDROID)
     SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${SEC_LINK_FLAGS}")
     SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${SEC_LINK_FLAGS}")
 ENDIF ()
+ENDIF ()
 
 # Clang Format support
 IF (UNIX OR APPLE)
@@ -464,9 +478,11 @@ IF (NOT ANDROID)
         ki18n_install("${CMAKE_CURRENT_BINARY_DIR}/po")
     else()
         ki18n_install(po)
-        find_package(KF5DocTools CONFIG)
-        if(KF5DocTools_FOUND)
-            kdoctools_install(po)
+        if (BUILD_DOC)
+            find_package(KF5DocTools CONFIG)
+            if(KF5DocTools_FOUND)
+                kdoctools_install(po)
+            endif()
         endif()
     endif()
 ENDIF ()
diff --git a/kstars/auxiliary/thememanager.cpp b/kstars/auxiliary/thememanager.cpp
index ffe7dba..c99fb52 100644
--- a/kstars/auxiliary/thememanager.cpp
+++ b/kstars/auxiliary/thememanager.cpp
@@ -154,9 +154,11 @@ void Manager::slotChangePalette()
     if (theme == "Macintosh" || theme == "White Balance" || theme == "High Key" || (theme == "Default"
             && currentDesktopdefaultTheme().contains("Dark") == false))
         themeIconType = BREEZE_THEME;
-
+#if defined(Q_OS_HAIKU)
+	if (theme == "Default")
+		themeIconType = SYSTEM_THEME;
     setIconTheme(themeIconType);
-
+#endif
     QString filename        = d->themeMap.value(theme);
     KSharedConfigPtr config = KSharedConfig::openConfig(filename);
     // hint for the style to synchronize the color scheme with the window manager/compositor
@@ -178,6 +180,10 @@ void Manager::slotChangePalette()
 
     if(theme == "Macintosh")
         qApp->setStyle(QStyleFactory::create("macintosh"));
+#if defined(Q_OS_HAIKU)
+	else if(theme == "Default")
+		qApp->setStyle(QStyleFactory::create("haiku"));
+#endif
     else
     {
         if (qgetenv("XDG_CURRENT_DESKTOP") != "KDE")
@@ -414,6 +420,12 @@ void Manager::setIconTheme(IconTheme theme)
     QString resourcePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "icons",
                            QStandardPaths::LocateDirectory) + QDir::separator() + iconTheme + QDir::separator() + rccFile;
     QResource::registerResource(resourcePath, "/icons/" + iconTheme);
+#elif defined(Q_OS_HAIKU)
+	if (theme == SYSTEM_THEME)
+		iconTheme = "haiku";
+	themeSearchPaths = themeSearchPaths << QStandardPaths::locate(QStandardPaths::GenericDataLocation, "icons", QStandardPaths::LocateDirectory);
+	QString resourcePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "icons", QStandardPaths::LocateDirectory) + QDir::separator() + iconTheme + QDir::separator() + rccFile;
+	QResource::registerResource(resourcePath, "/icons/" + iconTheme);
 #else
     // On Linux on non-KDE Distros, find out if the themes are installed or not and perhaps warn the user
     // The important point is that the current theme must be left as is to avoid empty icons
diff --git a/kstars/auxiliary/thememanager.h b/kstars/auxiliary/thememanager.h
index 8eafbfa..09bab99 100644
--- a/kstars/auxiliary/thememanager.h
+++ b/kstars/auxiliary/thememanager.h
@@ -42,7 +42,7 @@ class Manager : public QObject
 
 public:
 
-    typedef enum { BREEZE_THEME, BREEZE_DARK_THEME } IconTheme;
+    typedef enum { BREEZE_THEME, BREEZE_DARK_THEME, SYSTEM_THEME } IconTheme;
 
     ~Manager();
     static Manager* instance();
diff --git a/kstars/htmesh/SpatialGeneral.h b/kstars/htmesh/SpatialGeneral.h
index fa3ae42..660498a 100644
--- a/kstars/htmesh/SpatialGeneral.h
+++ b/kstars/htmesh/SpatialGeneral.h
@@ -101,7 +101,7 @@ const float64 gEpsilon = 1.0E-15;
 // standard explicit template instantiation, define.
 //
 
-#if defined(__APPLE__)
+#if defined(__APPLE__) || defined(__HAIKU__)
 #define LINKAGE
 #endif
 
diff --git a/kstars/kstars.cpp b/kstars/kstars.cpp
index 3796dc8..46ff7be 100644
--- a/kstars/kstars.cpp
+++ b/kstars/kstars.cpp
@@ -71,7 +71,7 @@ KStars::KStars(bool doSplash, bool clockrun, const QString &startdate)
     setWindowTitle(i18nc("@title:window", "KStars"));
 
     // Set thread stack size to 32MB
-#if QT_VERSION >= QT_VERSION_CHECK(5,10,0)
+#if QT_VERSION >= QT_VERSION_CHECK(5,10,0) && !defined(Q_OS_HAIKU)
     QThreadPool::globalInstance()->setStackSize(33554432);
 #endif
 
-- 
2.30.2

