From f11bac05a56dceb209d62a56f7cc86d10e6d5035 Mon Sep 17 00:00:00 2001
From: sirko805 <sirko805@tlvm-4-2-4.ad.liu.se>
Date: Thu, 29 Sep 2022 14:48:28 +0200
Subject: [PATCH] cmake

---
 build-system/CMakeLists.txt         | 17 +++++++++++++++++
 build-system/MainProgramConfig.h.in |  4 ++++
 build-system/a                      |  1 +
 build-system/b                      |  0
 build-system/copyfile.c             | 13 +++++++++++--
 build-system/main.cpp               | 13 +++++++++++--
 6 files changed, 44 insertions(+), 4 deletions(-)
 create mode 100644 build-system/MainProgramConfig.h.in
 create mode 100644 build-system/a
 create mode 100644 build-system/b

diff --git a/build-system/CMakeLists.txt b/build-system/CMakeLists.txt
index e69de29..85f8714 100644
--- a/build-system/CMakeLists.txt
+++ b/build-system/CMakeLists.txt
@@ -0,0 +1,17 @@
+cmake_minimum_required(VERSION 3.10)
+
+# set the project name
+project(MainProgram)
+option(TRIG_FUNCTIONS "Use trig func" ON)
+set(IS_HOST_WINDOWS,ON)
+configure_file(MainProgramConfig.h.in MainProgramConfig.h)
+add_library(copyfile SHARED copyfile.c)
+if(${CMAKE_HOST_SYSTEM_NAME}!="Windows")
+set(IS_HOST_WINDOWS,OFF)
+endif()
+# add the executable
+add_executable(MainProgram main.cpp)
+target_link_libraries(MainProgram ${CMAKE_DL_LIBS} copyfile)
+target_include_directories(MainProgram PUBLIC
+                           "${PROJECT_BINARY_DIR}"
+                           )
\ No newline at end of file
diff --git a/build-system/MainProgramConfig.h.in b/build-system/MainProgramConfig.h.in
new file mode 100644
index 0000000..14bae43
--- /dev/null
+++ b/build-system/MainProgramConfig.h.in
@@ -0,0 +1,4 @@
+#define CopyFile_VERSION_MAJOR @CopyFile_VERSION_MAJOR@
+#define CopyFile_VERSION_MINOR @CopyFile_VERSION_MINOR@
+#cmakedefine TRIG_FUNCTIONS
+#cmakedefine IS_HOST_WINDOWS
\ No newline at end of file
diff --git a/build-system/a b/build-system/a
new file mode 100644
index 0000000..d35bea1
--- /dev/null
+++ b/build-system/a
@@ -0,0 +1 @@
+amongus
\ No newline at end of file
diff --git a/build-system/b b/build-system/b
new file mode 100644
index 0000000..e69de29
diff --git a/build-system/copyfile.c b/build-system/copyfile.c
index 3a26033..ad8dd5b 100644
--- a/build-system/copyfile.c
+++ b/build-system/copyfile.c
@@ -1,11 +1,20 @@
 #include <stdio.h>
-
+#include <string.h>
+#include <dlfcn.h>
+#include "MainProgramConfig.h"
+#include <fcntl.h>
+#if !(IS_HOST_WINDOWS)
+#include <linux/fs.h>
+#include <sys/ioctl.h>
+#endif
+#include <errno.h>
 void copy_files()
 {
   int dest_fd, src_fd;
+  printf("lib called\n");
   src_fd = open("a", O_RDONLY);
   dest_fd = open("b", O_WRONLY); /* Needs to exist; not checking this */
-#if 1
+#if !(IS_HOST_WINDOWS)
   printf("Using FICLONE\n");
   if (ioctl(dest_fd, FICLONE, src_fd) < 0) {
     fprintf(stderr, "error: %s\n", strerror(errno));
diff --git a/build-system/main.cpp b/build-system/main.cpp
index b43c749..2f4f7b7 100644
--- a/build-system/main.cpp
+++ b/build-system/main.cpp
@@ -1,15 +1,24 @@
 #include <stdio.h>
+#include <dlfcn.h>
+#include <math.h>
+#include "MainProgramConfig.h"
 
 int main(int argc, char **argv)
 {
+  void (*cpfileLib)();
   /* Note: dlopen does not exist on Windows and the filename will not be .so.
    * If you are performing the labs on Windows, you need to change this.
    */
   void *ptr = dlopen("libcopyfile.so", RTLD_LAZY);
+  if (ptr != NULL) {
+  cpfileLib = (void(*)()) dlsym(ptr,"copy_files");
+  cpfileLib();
+  }
   /* TODO: Call copy_files() from the opened shared object */
 
-  { /* Disable this if --without-trig-functions or -DTRIG_FUNCTIONS:BOOL=OFF */
+  #ifdef TRIG_FUNCTIONS
+   /* Disable this if --without-trig-functions or -DTRIG_FUNCTIONS:BOOL=OFF */
     double d = cos(1.5);
     printf("The result of cos (a trigonometric function) is: %f\n", d);
-  }
+  #endif
 }
-- 
GitLab