Chi-Tech
devman_01_02_usinglib.h
Go to the documentation of this file.
1/**\page DevManUsingLib Using Chi-Tech as a library
2 *
3 *
4 * \section devman01_02 So you want to use Chi-Tech as a library
5 *
6 * \subsection devman01_02_01 Step 1 - Make a application directory and empty source file
7 *
8 * Wherever you like, create a folder that will contain your application.
9 *
10 * \code
11 * mkdir TestApp
12 * cd TestApp
13 * \endcode
14 *
15 * Now create an empty source file
16 *
17 * \code
18 * >test.cc
19 * \endcode
20 *
21 * ## _
22 * \subsection devman01_02_02 Step 2 - Create CMakeLists.txt file
23 *
24 * In the folder you just created create a text file called `CMakeLists.txt`
25 *
26 * \code
27 * >CMakeLists.txt
28 * \endcode
29 *
30 * ## _
31 * \subsection devman01_02_03 Step 3 - Edit the cmake file to connect to Chi-Tech
32 *
33 * We need to specify a few values to be included in the `CMakeLists.txt` file.
34 * - The desired application/executable name, for now we will call that <B>test</B>.
35 * - The Chi-Tech "downstream" cmake include, `Downstream.cmake`.
36 *
37\code
38cmake_minimum_required(VERSION 3.12)
39
40set(TARGET test_app)
41project(test CXX)
42
43#------------------------------------------------ DEPENDENCIES
44if (NOT DEFINED CHI_TECH_DIR)
45 if (NOT (DEFINED ENV{CHI_TECH_DIR}))
46 message(FATAL_ERROR "***** CHI_TECH_DIR is not set *****")
47 else()
48 set(CHI_TECH_DIR "$ENV{CHI_TECH_DIR}")
49 endif()
50endif()
51message(STATUS "CHI_TECH_DIR set to ${CHI_TECH_DIR}")
52
53include("${CHI_TECH_DIR}/resources/CMakeMacros/Downstream.cmake")
54
55set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/lib")
56set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/lib")
57set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin")
58
59file (GLOB_RECURSE SOURCES "*.cc")
60add_executable(${TARGET} "${SOURCES}")
61if(UNIX AND NOT APPLE)
62 target_link_libraries(${TARGET} ${CHI_LIBS} -Wl,--whole-archive ChiLib -Wl,--no-whole-archive )
63elseif(APPLE)
64 target_link_libraries(${TARGET} ${CHI_LIBS} -Wl,-all_load ChiLib )
65endif()
66
67file(WRITE ${PROJECT_SOURCE_DIR}/Makefile "subsystem:\n" "\t$(MAKE) -C chi_build \n\n"
68 "clean:\n\t$(MAKE) -C chi_build clean\n")
69\endcode
70
71The `set(TARGET test_app)` line sets the name of the eventual executable
72to be used. You can use any name other than `test`.
73The `include` statement allows you to connect to all the resources connected
74to Chi-Tech, including lua, PETSc, etc.
75Make sure that this environment variable is set.
76You will have to find the location where you compiled Chi-Tech in order to
77properly specify the location of `Downstream.cmake`. Once this is properly
78specified, the cmake-variable `CHI_LIBS` will be defined and the necessary
79include-files will be usable.
80 The line `file (GLOB_RECURSE SOURCES "*.cc")` adds all `.cc` files,
81 in the current directory, to
82 the list of sources to compile. To specify specific
83 source-file names use `set(SOURCES "test.cc")`. Alternatively cmake
84 functionality can be googled to determine how to add sub-directories.
85
86## _
87
88\subsection devman01_02_04 Step 4 - Add the basic code to test.cc
89
90
91\code
92#include "chi_runtime.h"
93#include "chi_log.h"
94
95int main(int argc, char* argv[])
96{
97 chi::Initialize(argc,argv);
98 chi::RunBatch(argc, argv);
99
100 chi::log.Log() << "Hello World!";
101
102 //We will add code here
103
104 chi::Finalize();
105 return 0;
106}
107\endcode
108
109This code is the minimum needed to have everything available in Chi-Tech.
110The basic namespace access is provided via `#include "chi_runtime.h"`
111
112MPI initialization and PETSc initialization is handled via the call to
113`chi::Initialize()`.
114
115Finally all MPI and PETSc related items are destroyed via the call to
116`chi::Finalize()`.
117
118## _
119
120\subsection devman01_02_05 Step 5 - Compile the code
121Well, we should probably state here that you should make sure ChiTech compiled.
122This can be done by running the test cases. If all is well then proceed to make
123build directory (```TestApp/build```) for this app.
124
125\code
126mkdir build
127cd build
128\endcode
129
130Next execute cmake from this directory, giving it the directory-location of the
131CMakeLists.txt created in \ref devman01_02_02 "step 2".
132
133\code
134cmake ../
135\endcode
136
137If all is well here then simply run make:
138
139\code
140make -j4
141\endcode
142
143##_
144
145 * \section devman01_03 Adding lua-input to library-using apps
146 * For this section we refer to \ref devman01_02 "the section above" where we
147 * created a test application.
148 *
149 * \subsection devman01_03_01 Step 1 - Create folder to contain lua sources.
150 * Purely for categorization, create a ```lua``` folder in the ```TestApp```
151 * folder
152 *
153 * \code
154 * cd ../TestApp
155 * mkdir lua
156 * cd lua
157 * \endcode
158 *
159 * \subsection devman01_03_02 Step 2 - Create a source file
160 * Let us pretend that we will have a status update message that we want to be
161 * printed from a lua call. Create a file called ```testapp_statusmess.cc```
162 * with the following contents:
163 *
164 * \code
165 * #include "chi_lua.h"
166 * #include "chi::log.h"
167 *
168 * int chiPrintStatus(lua_State *L)
169 * {
170 * ChiLog& log = ChiLog::GetInstance();
171 *
172 * log.Log() << "Hello from here";
173 *
174 * return 0;
175 * }
176 * \endcode
177 *
178 * ## _
179 *
180 * \subsection devman01_03_03a Step 3 - Modify the CMakeLists.txt file to compile this code
181 * In preparation for adding many more sources to the ```lua``` folder, edit the
182 * CMakeLists.txt file as follows:
183 *
184 *
185 *
186\code{cmake}
187cmake_minimum_required(VERSION 3.2)
188
189SET(TARGET test)
190project(${TARGET} C CXX)
191
192include(~/Desktop/ChiTech/chi-tech/CHI_RESOURCES/Macros/Downstream.cmake)
193include(~/Desktop/ChiTech/chi-tech/CHI_RESOURCES/Macros/Filter.cmake)
194
195set(SOURCES "test.cc" )
196file (GLOB_RECURSE MORE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/lua/*.cc")
197
198set(SOURCES ${SOURCES} ${MORE_SOURCES})
199
200add_executable(${TARGET} "${SOURCES}")
201target_link_libraries(${TARGET} ${CHI_LIBS})
202\endcode
203This will allow any ```.cc``` file in the ```lua``` folder to be compiled with
204the project. The line
205
206
207 \code{cmake}
208 file (GLOB_RECURSE MORE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/lua/*.cc")
209 \endcode
210
211
212
213 recursively searches through the specified folder and pumps any filenames
214 ending with .cc into the variable ```MORE_SOURCES```. This variable gets added
215 to the manually specified ```SOURCES``` variable using the line
216
217 \code
218 set(SOURCES ${SOURCES} ${MORE_SOURCES})
219 \endcode
220
221
222 ## _
223 * \subsection devman01_03_04a Step 4 - Modify test.cc to register the lua function
224 * Modify your ```test.cc``` source file to be
225\code
226#include "chi_runtime.h"
227#include "console/chi_console.h"
228
229int main(int argc, char* argv[])
230{
231 ChiTech::Initialize(argc,argv);
232
233 ChiConsole& console = ChiConsole::GetInstance();
234
235 auto L = console.consoleState;
236 #include "ChiMacros/lua_register_macro.h"
237
238 RegisterFunction(chiPrintStatus);
239
240 ChiTech::Finalize();
241 return 0;
242}
243\endcode
244
245Notice here that we firstly included the header file for the ```ChiConsole```
246object. This is the object that allows us to connect to the lua state.
247
248\code
249#include "ChiConsole/chi_console.h"
250\endcode
251
252
253
254Next we obtained a reference to the console through
255
256\code
257ChiConsole& console = ChiConsole::GetInstance();
258\endcode
259
260
261 We then setup the variable ```L```, which is needed by the macro file
262 ``` ChiMacros/lua_register_macro.h ```.
263
264
265\code
266auto L = console.consoleState;
267#include "ChiMacros/lua_register_macro.h"
268\endcode
269
270
271
272 This macro file exposes numerous lua macros, one of which is the
273 ```RegisterFunction(x)``` macro where we will supply the ```x``` as the
274 function call we defined earlier:
275
276 \code
277 RegisterFunction(chiPrintStatus);
278 \endcode
279 * */