StaticGenerators.xtend 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 generateCMakeLists(String projectName, String frameworkPath)
  118. {
  119. '''
  120. cmake_minimum_required (VERSION 2.8.5)
  121. project («projectName» C CXX)
  122. include(CheckCXXCompilerFlag)
  123. #set(CMAKE_VERBOSE_MAKEFILE on)
  124. CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
  125. CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
  126. if(COMPILER_SUPPORTS_CXX11)
  127. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  128. elseif(COMPILER_SUPPORTS_CXX0X)
  129. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
  130. else()
  131. message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
  132. endif()
  133. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFMI_COSIMULATION -g")
  134. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFMI_COSIMULATION -g")
  135. include_directories(src)
  136. file(GLOB CPP_FILES src/*.cpp)
  137. file(GLOB CPP_MAIN_FILES src/main*.cpp)
  138. list(REMOVE_ITEM CPP_FILES ${CPP_MAIN_FILES})
  139. ## library ##
  140. add_library(${PROJECT_NAME} SHARED ${CPP_FILES} ${CPP_FILES})
  141. target_link_libraries(${PROJECT_NAME} hcf)
  142. set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
  143. ## Main executable ##
  144. add_executable(${PROJECT_NAME}_main ${CPP_MAIN_FILES} ${CPP_FILES})
  145. target_link_libraries(${PROJECT_NAME}_main hcf)
  146. foreach(LETTER ${CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES})
  147. if(EXISTS "${LETTER}/libstdc++.a")
  148. # message (STATUS "${LETTER}")
  149. set(STDCPP_LIBRARY "${LETTER}/libstdc++.a")
  150. else()
  151. # message (STATUS "${LETTER} ---- not these")
  152. endif()
  153. endforeach()
  154. message(STATUS "Static linking with libstdc++.a in ${STDCPP_LIBRARY}")
  155. #target_link_libraries(${PROJECT_NAME} ${STDCPP_LIBRARY})
  156. # I need this: -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic
  157. if (WIN32)
  158. #windows
  159. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libstdc++ -static-libgcc -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic")
  160. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic")
  161. endif()
  162. #if(NOT APPLE)
  163. #https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/pthread.3.html
  164. #it is included in system
  165. find_library(PTHREAD_LIBRARY NAMES libwinpthread.dll.a)# libpthread.a libpthread.dylib )
  166. #message( INFO ${PTHREAD_LIBRARY})
  167. #target_link_libraries(${PROJECT_NAME}_main ${PTHREAD_LIBRARY} /C/msys64/mingw64/x86_64-w64-mingw32/lib/libwinpthread.a)
  168. #endif()
  169. add_subdirectory («frameworkPath»)
  170. #add_custom_command(${PROJECT_NAME}_main POST_BUILD COMMAND "find . -name *.exe -exec echo {} \; -exec bash -c \"objdump -p {} | grep 'DLL Name:'\" \;")
  171. '''
  172. }
  173. }