StaticGenerators.xtend 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package be.uantwerpen.ansymo.semanticadaptation.testframework
  2. class StaticGenerators {
  3. def static String generateMainCppFile(String folderPath)
  4. {
  5. '''
  6. /*
  7. * main.cpp
  8. *
  9. * Created on: Mar 14, 2017
  10. * Author: kel
  11. */
  12. #include <iostream>
  13. #include "fmi2TypesPlatform.h"
  14. #include "fmi2Functions.h"
  15. #include <string>
  16. #include <memory>
  17. #include <iostream>
  18. #include <cstdio>
  19. #include <stdarg.h> /* va_list, va_start, va_arg, va_end */
  20. #include <fstream>
  21. using namespace std;
  22. void fmuLoggerCache(void *componentEnvironment, fmi2String instanceName,
  23. fmi2Status status, fmi2String category, fmi2String message, ...) {
  24. va_list argp;
  25. // replace C format strings
  26. va_start(argp, message);
  27. size_t size = vsnprintf(nullptr, 0, message, argp) + 1; // Extra space for '\0'
  28. va_end(argp);
  29. va_start(argp, message);
  30. unique_ptr<char[]> buf(new char[size]);
  31. vsnprintf(buf.get(), size, message, argp);
  32. va_end(argp);
  33. string completeMessage = string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
  34. cout << "FROM MAIN: Name: " << instanceName << " Status: " << status << " Category: "
  35. << category << " Msg: " << completeMessage << endl;
  36. }
  37. fmi2Status setReal(fmi2Component comp, fmi2ValueReference id, fmi2Real val) {
  38. const fmi2ValueReference vr[] { id };
  39. size_t nvr = 1;
  40. fmi2Real value[] { val };
  41. return fmi2SetReal(comp, vr, nvr, value);
  42. }
  43. fmi2Real getReal(fmi2Component comp, fmi2ValueReference id) {
  44. const fmi2ValueReference vr[] { id };
  45. size_t nvr = 1;
  46. fmi2Real value[1];
  47. fmi2GetReal(comp, vr, nvr, value);
  48. return value[0];
  49. }
  50. int main(int argc, char *argv[]) {
  51. // setvbuf(stdout, NULL, _IONBF, 0);
  52. // setvbuf(stderr, NULL, _IONBF, 0);
  53. cout << "hello" << endl;
  54. fmi2CallbackFunctions callback = { &fmuLoggerCache, NULL, NULL, NULL, NULL };
  55. fmi2Component comp =
  56. fmi2Instantiate("this system", fmi2CoSimulation, "{1234}",
  57. "«folderPath»",
  58. &callback, fmi2True,
  59. fmi2True);
  60. if (comp == NULL) {
  61. cout << "init failed" << endl;
  62. return -1;
  63. }
  64. if (fmi2SetupExperiment(comp, false, 0.0, 0, true, 100) != fmi2OK) {
  65. printf("Error fmi2SetupExperiment");
  66. return 1;
  67. }
  68. if (fmi2EnterInitializationMode(comp) != fmi2OK) {
  69. printf("Error fmi2SetupExperiment");
  70. return 1;
  71. }
  72. if (fmi2ExitInitializationMode(comp) != fmi2OK) {
  73. printf("Error fmi2SetupExperiment");
  74. return 1;
  75. }
  76. #define ID_Window_SA_IN_reaction_force 0
  77. #define ID_Window_SA_IN_displacement 1
  78. #define ID_Window_SA_IN_speed 2
  79. #define ID_Window_SA_OUT_disp 3
  80. #define ID_Window_SA_OUT_tau 4
  81. std::fstream fs;
  82. fs.open("result.csv",
  83. std::fstream::in | std::fstream::out | std::fstream::trunc);
  84. //cout << "------------ Header ---------" << endl;
  85. fs << "\"time\",\"ID_Window_SA_OUT_disp\",\"ID_Window_SA_OUT_tau\"" << endl;
  86. double time = 0.0;
  87. double stepSize = 0.01;
  88. for (double time = 0.0; time < 10;) {
  89. if (setReal(comp, ID_Window_SA_IN_reaction_force, -1.0) != fmi2OK) {
  90. printf("Error setting real");
  91. return 1;
  92. }
  93. if (setReal(comp, ID_Window_SA_IN_displacement, time) != fmi2OK) {
  94. printf("Error setting real");
  95. return 1;
  96. }
  97. if (setReal(comp, ID_Window_SA_IN_speed, 1.0) != fmi2OK) {
  98. printf("Error setting real");
  99. return 1;
  100. }
  101. if (fmi2DoStep(comp, time, stepSize, false) != fmi2OK) {
  102. printf("Errorin do step");
  103. return 1;
  104. }
  105. time += stepSize;
  106. auto disp = getReal(comp, ID_Window_SA_OUT_disp);
  107. auto tau = getReal(comp, ID_Window_SA_OUT_tau);
  108. cout << "time: " << time << " disp: " << disp << " tau: " << tau
  109. << endl;
  110. fs << time << "," << disp << "," << tau << endl;
  111. }
  112. fs.close();
  113. return 0;
  114. }
  115. '''
  116. }
  117. def static String generateToolChainCmake(){
  118. '''
  119. # the name of the target operating system
  120. SET(CMAKE_SYSTEM_NAME Windows)
  121. SET(WIN32 true)
  122. # which compilers to use for C and C++
  123. SET(CMAKE_C_COMPILER /mingw64/bin/gcc)
  124. SET(CMAKE_CXX_COMPILER /mingw64/bin/g++)
  125. #SET(CMAKE_RC_COMPILER i486-mingw32-windres)
  126. # here is the target environment located
  127. #SET(CMAKE_FIND_ROOT_PATH /usr/i486-mingw32)
  128. # adjust the default behaviour of the FIND_XXX() commands:
  129. # search headers and libraries in the target environment, search
  130. # programs in the host environment
  131. #set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  132. #set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  133. #set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  134. '''
  135. }
  136. def static String generateCMakeLists(String projectName, String frameworkPath)
  137. {
  138. '''
  139. #
  140. # #%~
  141. # The Overture Abstract Syntax Tree
  142. # %%
  143. # Copyright (C) 2017 - 2014 Aarhus University
  144. # %%
  145. # This program is free software: you can redistribute it and/or modify
  146. # it under the terms of the GNU General Public License as
  147. # published by the Free Software Foundation, either version 3 of the
  148. # License, or (at your option) any later version.
  149. #
  150. # This program is distributed in the hope that it will be useful,
  151. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  152. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  153. # GNU General Public License for more details.
  154. #
  155. # You should have received a copy of the GNU General Public
  156. # License along with this program. If not, see
  157. # <http://www.gnu.org/licenses/gpl-3.0.html>.
  158. # #~%
  159. #
  160. cmake_minimum_required (VERSION 3.5.2)
  161. # this (3.5.2) is the first version where External Project uses --recursive
  162. project («projectName» C CXX)
  163. # HOW to
  164. #
  165. # cmake -DCMAKE_TOOLCHAIN_FILE=msys-toolchain.cmake -DCMAKE_BUILD_TYPE=Debug .
  166. # make
  167. #
  168. # to list dependencies use:
  169. # objdump -p binaries/win64/window-sa.dll | grep 'DLL Name:'
  170. #
  171. if (WIN32)
  172. #windows, becuase windows just cannot figure out to do it correct.
  173. # must be a bug in msys mingw gcc 6.3
  174. # it doesnt read the rsp files.
  175. set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES 0)
  176. set(CMAKE_C_USE_RESPONSE_FILE_FOR_INCLUDES 0)
  177. set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_LIBRARIES 0)
  178. set(CMAKE_C_USE_RESPONSE_FILE_FOR_LIBRARIES 0)
  179. endif()
  180. add_subdirectory(«frameworkPath»)
  181. include(CheckCXXCompilerFlag)
  182. CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
  183. CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
  184. if(COMPILER_SUPPORTS_CXX11)
  185. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  186. elseif(COMPILER_SUPPORTS_CXX0X)
  187. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
  188. else()
  189. message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
  190. endif()
  191. add_definitions(-DFMI_COSIMULATION)
  192. file(GLOB CPP_FILES sources/*.cpp)
  193. file(GLOB CPP_MAIN_FILES sources/main*.cpp)
  194. list(REMOVE_ITEM CPP_FILES ${CPP_MAIN_FILES})
  195. ## library ##
  196. #determine the library path
  197. set(FMI_BIN_DIR "${CMAKE_CURRENT_SOURCE_DIR}/binaries")
  198. if(WIN32 OR MINGW)
  199. set(FMI_BIN_DIR "${FMI_BIN_DIR}/win")
  200. # not sure why this doesnt work on MSYS so we just reset it
  201. SET (CMAKE_SYSTEM_PROCESSOR "$ENV{PROCESSOR_ARCHITECTURE}")
  202. endif()
  203. if(APPLE)
  204. set(FMI_BIN_DIR "${FMI_BIN_DIR}/darwin")
  205. endif()
  206. if(UNIX AND NOT APPLE)
  207. # for Linux, BSD, Solaris, Minix
  208. set(FMI_BIN_DIR "${FMI_BIN_DIR}/linux")
  209. endif()
  210. if (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)")
  211. set(FMI_BIN_DIR "${FMI_BIN_DIR}64")
  212. else ()
  213. set(FMI_BIN_DIR "${FMI_BIN_DIR}32")
  214. endif ()
  215. message("FMI output is ${FMI_BIN_DIR}")
  216. #SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "")
  217. file(MAKE_DIRECTORY ${FMI_BIN_DIR})
  218. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${FMI_BIN_DIR})
  219. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${FMI_BIN_DIR})
  220. #set(CMAKE_LIBRARY_OUTPUT_DIRECTORY bb)
  221. #set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${FMI_BIN_DIR})
  222. add_library(${PROJECT_NAME} SHARED ${CPP_FILES})
  223. target_link_libraries(${PROJECT_NAME} PUBLIC hcf)
  224. set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
  225. SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "")
  226. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
  227. ## Main executable ##
  228. add_executable(${PROJECT_NAME}_main ${CPP_MAIN_FILES} ${CPP_FILES})
  229. target_link_libraries(${PROJECT_NAME}_main PUBLIC hcf)
  230. # I need this: -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic
  231. if (WIN32)
  232. #windows
  233. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic")
  234. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic")
  235. endif()
  236. '''
  237. }
  238. }