Skip to content

Commit 1f2451e

Browse files
Merge pull request #1630 from Arnaud-de-Grandmaison-ARM/matrix-gitlab
[Matrix] Code examples have been moved to the gitlab repository.
2 parents 0147049 + 3c48471 commit 1f2451e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+190
-2258
lines changed

content/learning-paths/cross-platform/matrix/1-foundations.md

Lines changed: 99 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ Emacs](https://www.gnu.org/software/emacs/), or [Sublime
3232
Text](https://www.sublimetext.com/), which are also popular and they all
3333
support extensions that make C++ development easy.
3434

35+
## Source code
36+
37+
In case you want to, you can [download the source code](https://gitlab.arm.com/learning-code-examples/code-examples/-/archive/main/code-examples-main.tar.gz?path=learning-paths/cross-platform/matrix) for this learning path. This will download a `.tar.gz` archive that you will need to expand:
38+
39+
```BASH
40+
tar xfz code-examples-main-learning-paths-cross-platform-matrix.tar.gz
41+
mv code-examples-main-learning-paths-cross-platform-matrix code-examples
42+
```
43+
44+
The source code for this learning path will be available in `code-examples/learning-paths/cross-platform/matrix/`.
3545

3646
## What are the differences between configuring the project and building the code?
3747

@@ -64,7 +74,7 @@ projects like [LLVM](https://www.llvm.org) or [Qt](https://www.qt.io/).
6474

6575
Organizing the files in a project is important because it allows you to:
6676

67-
- Easily navigate the structure and find information.
77+
- Easily navigate the structure and find information.
6878
- Organize information for the tools, such as compilers and linkers.
6979
- Make a distinction between information that is exported or installed, and what is
7080
only relevant for building the project.
@@ -117,7 +127,18 @@ There is nothing like creating the canonical `Hello, World!` application!
117127
Use your favorite text editor or IDE to
118128
create the file `src/howdy.cpp` and add the following content:
119129

120-
{{< include-code CPP "content/learning-paths/cross-platform/matrix/projects/chapter-1/src/howdy.cpp" >}}
130+
```CPP
131+
#include <cstdlib>
132+
#include <iostream>
133+
134+
using namespace std;
135+
136+
int main(int argc, char *argv[]) {
137+
cout << "Hello, World !\n";
138+
139+
return EXIT_SUCCESS;
140+
}
141+
```
121142
122143
## Setup CMake
123144
@@ -226,21 +247,89 @@ library version.
226247
Add the `Matrix.h` header file, declaring the `Version` object
227248
and the `getVersion` function and save the file as `include/Matrix/Matrix.h`:
228249

229-
{{< include-code CPP "content/learning-paths/cross-platform/matrix/projects/chapter-1/include/Matrix/Matrix.h" >}}
250+
```CPP
251+
#pragma once
252+
253+
namespace MatComp {
254+
255+
/// The Version struct is used to carry around the major, minor and patch level.
256+
struct Version {
257+
unsigned major; //< The major version level.
258+
unsigned minor; //< The minor version level.
259+
unsigned patch; //< The patch level.
260+
};
261+
262+
/// Get the Matrix library version information.
263+
const Version &getVersion();
264+
265+
} // namespace MatComp
266+
```
230267
231268
With those declarations in place, create and add the following lines to
232269
`lib/Matrix/Matrix.cpp` to provide an implementation to `getVersion`:
233270
234-
{{< include-code CPP "content/learning-paths/cross-platform/matrix/projects/chapter-1/lib/Matrix/Matrix.cpp" >}}
271+
```CPP
272+
#include "Matrix/Matrix.h"
273+
274+
namespace {
275+
const MatComp::Version version = {.major = 0, .minor = 1, .patch = 0};
276+
}
277+
278+
namespace MatComp {
279+
280+
const Version &getVersion() { return version; }
281+
282+
} // namespace MatComp
283+
```
235284

236285
Now, you can create a program that will make use of the
237286
``getVersion`` function. Use your editor to save the code below as `src/getVersion.cpp`:
238287

239-
{{< include-code CPP "content/learning-paths/cross-platform/matrix/projects/chapter-1/src/getVersion.cpp" >}}
288+
```CPP
289+
#include "Matrix/Matrix.h"
290+
291+
#include <cstdlib>
292+
#include <iostream>
293+
294+
using namespace std;
295+
using namespace MatComp;
296+
297+
int main(int argc, char *argv[]) {
298+
const Version &version = getVersion();
299+
cout << "Using Matrix version: " << version.major << '.' << version.minor
300+
<< '.' << version.patch << '\n';
301+
302+
return EXIT_SUCCESS;
303+
}
304+
```
240305
241306
Finally, add the instructions below in the top-level `CMakeLists.txt`:
242307
243-
{{< include-code TXT "content/learning-paths/cross-platform/matrix/projects/chapter-1/CMakeLists.txt" >}}
308+
```TXT
309+
# Set the minimum CMake version we require. In our case, it is intentionnally
310+
# very old as we are not making use of recent CMake features.
311+
cmake_minimum_required(VERSION 3.5)
312+
313+
# Give a name to our project ('Matrix') and inform CMake about the language used.
314+
project(Matrix LANGUAGES CXX)
315+
316+
# Add 'howdy', a standalone executable with no dependency to any library that
317+
# has to be built from the sources in 'src/howdy.cpp'.
318+
add_executable(howdy src/howdy.cpp)
319+
320+
# Add our 'Matrix' library, that is built as a static library, from source file
321+
# 'lib/Matrix/Matrix.cpp'. CMake is instruction that C++17 is used, and that
322+
# the library headers can be found in ${CMAKE_SOURCE_DIR}/include.
323+
add_library(Matrix STATIC lib/Matrix/Matrix.cpp)
324+
target_compile_features(Matrix PUBLIC cxx_std_17)
325+
target_include_directories(Matrix
326+
PUBLIC ${CMAKE_SOURCE_DIR}/include)
327+
328+
# Add 'matrix-getVersion', an executable that depends on the Matrix library,
329+
# that has to be built from source file 'src/getVersion.cpp'.
330+
add_executable(matrix-getVersion src/getVersion.cpp)
331+
target_link_libraries(matrix-getVersion Matrix)
332+
```
244333

245334
The `add_library` instructs CMake how to build the Matrix library. The
246335
`target_include_directories` specifies where the Matrix library header is located, and the `target_compile_features` specifies that C++17 is the version
@@ -301,3 +390,7 @@ For example, Visual Studio Code can work seamlessly with CMake with plugins, and
301390
generate project files for several popular IDEs, such as Xcode, Sublime Text, Eclipse,
302391
CodeBlocks, and CodeLite. You can run `cmake --help` to get a
303392
list of supported *generators* (in CMake terminology) for your platform.
393+
394+
You can refer to this chapter source code in
395+
`code-examples/learning-paths/cross-platform/matrix/chapter-1` in the archive that
396+
you have downloaded earlier.

content/learning-paths/cross-platform/matrix/2-testing.md

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ benefits:
1717
- They offer an opportunity to catch regressions.
1818
- They demonstrate how to use the library in practice.
1919
- They create opportunities for those new to the project to easily check their patches, and verify that the introduction of the new code has not created unintended negative changes.
20-
20+
2121
You will notice that setting up testing precedes library code development.
2222

2323
There are many unit testing frameworks available, and C++ is not short of them. See this [wikipedia
@@ -38,7 +38,38 @@ all external dependencies. It will be used by the main `CMakeLists.txt`.
3838

3939
Create the file `external/CMakeLists.txt` with the following content:
4040

41-
{{< include-code TXT "content/learning-paths/cross-platform/matrix/projects/chapter-2/external/CMakeLists.txt" >}}
41+
```TXT
42+
cmake_minimum_required(VERSION 3.6)
43+
44+
project(external LANGUAGES CXX)
45+
46+
# Get the functionality to configure, build and install external project
47+
# from CMake module 'ExternalProject'.
48+
include(ExternalProject)
49+
50+
# Use the same compiler, build type and instalation directory than those
51+
# from our caller.
52+
set(EXTERNAL_PROJECT_CMAKE_ARGS
53+
-DCMAKE_CXX_COMPILER:PATH=${CMAKE_CXX_COMPILER}
54+
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
55+
-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX})
56+
57+
# Add 'googletext' as an external project, that will be cloned with git,
58+
# from the official googletest repository, at version v1.14.
59+
# We ask for a shallow clone, which is a clone with only the revision
60+
# we are interested in rather than googletest's full history ---
61+
# this makes the clone much faster (less data traffic), and uses much
62+
# less disk space ; furthermore, as we are not developping googletest
63+
# but just merely using it, we don't need thre full history. It will be
64+
# built and installed with our build configuration passed with CMAKE_ARGS.
65+
ExternalProject_Add(googletest
66+
PREFIX "external"
67+
GIT_REPOSITORY "https://github.com/google/googletest"
68+
GIT_TAG "v1.14.0"
69+
GIT_SHALLOW TRUE
70+
CMAKE_ARGS ${EXTERNAL_PROJECT_CMAKE_ARGS}
71+
)
72+
```
4273

4374
You might notice a new CMake feature: variables. Variables start with the `$` character and have a name inserted between curly braces. A CMake variable can be set by the CMake itself, or by the user, and they can be modified or used as they are.
4475

@@ -235,11 +266,33 @@ several files inside the `tests/` directory.
235266

236267
Create the top-level test in `tests/main.cpp` and paste the following code into the file:
237268

238-
{{< include-code CPP "content/learning-paths/cross-platform/matrix/projects/chapter-2/tests/main.cpp" >}}
269+
```CPP
270+
#include "gtest/gtest.h"
271+
272+
using namespace testing;
273+
274+
int main(int argc, char **argv) {
275+
InitGoogleTest(&argc, argv);
276+
return RUN_ALL_TESTS();
277+
}
278+
```
239279
240280
Create `tests/Version.cpp` and add the `getVersion` unit test into the file:
241281
242-
{{< include-code CPP "content/learning-paths/cross-platform/matrix/projects/chapter-2/tests/Version.cpp" >}}
282+
```CPP
283+
#include "Matrix/Matrix.h"
284+
285+
#include "gtest/gtest.h"
286+
287+
using namespace MatComp;
288+
289+
TEST(Matrix, getVersion) {
290+
const Version &version = getVersion();
291+
EXPECT_EQ(version.major, 0);
292+
EXPECT_EQ(version.minor, 1);
293+
EXPECT_EQ(version.patch, 0);
294+
}
295+
```
243296

244297
This test invokes `getVersion` and checks that the `major`, `minor` and `patch` levels match the expected values.
245298

@@ -325,3 +378,7 @@ Matrix/
325378
CMake makes it easy to use GoogleTest as an external project. Adding unit tests as you go is now easy.
326379

327380
You have created the unit testing environment for your Matrix library and added a test. The infrastructure is now in place to implement the core of the Matrix processing library.
381+
382+
You can refer to this chapter source code in
383+
`code-examples/learning-paths/cross-platform/matrix/chapter-2` in the archive that
384+
you have downloaded earlier.

content/learning-paths/cross-platform/matrix/3-code-1.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,28 @@ Open `lib/Matrix/Matrix.cpp` and include at the top of the file:
6262

6363
Add `die`'s body as shown below:
6464

65-
{{< include-code CPP "content/learning-paths/cross-platform/matrix/projects/chapter-3/lib/Matrix/Matrix.cpp" >}}
65+
```CPP
66+
#include "Matrix/Matrix.h"
67+
68+
#include <cstdlib>
69+
#include <iostream>
70+
71+
namespace {
72+
const MatComp::Version version = {.major = 0, .minor = 1, .patch = 0};
73+
}
74+
75+
namespace MatComp {
76+
77+
const Version &getVersion() { return version; }
78+
79+
void die(const char *fileName, size_t lineNumber, const char *reason) {
80+
std::cerr << "Fatal: " << reason << " from " << fileName << ':'
81+
<< lineNumber << '\n';
82+
exit(EXIT_FAILURE);
83+
}
84+
85+
} // namespace MatComp
86+
```
6687
6788
At this stage, the project should still build and compile, try it to confirm:
6889
@@ -1045,3 +1066,7 @@ After this rather long exercise, you have a minimalistic, yet fully-functional c
10451066
Modern C++ enables you to express move and copy semantics, and to use smart pointers to make memory management easy.
10461067

10471068
The compiler also catch a large number of type or misuse errors. With this core functionality in place, you have all you need to implement matrix operations in the next section.
1069+
1070+
You can refer to this chapter source code in
1071+
`code-examples/learning-paths/cross-platform/matrix/chapter-3` in the archive that
1072+
you have downloaded earlier.

content/learning-paths/cross-platform/matrix/4-code-2.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,3 +1150,7 @@ built and used.
11501150
The testing could - and *should* - go much deeper, as a number of corner cases have not been covered.
11511151

11521152
You can continue to add more functions, and more tests.
1153+
1154+
You can refer to this chapter source code in
1155+
`code-examples/learning-paths/cross-platform/matrix/chapter-4` in the archive that
1156+
you have downloaded earlier.

content/learning-paths/cross-platform/matrix/projects/chapter-1/.clang-format

Lines changed: 0 additions & 5 deletions
This file was deleted.

content/learning-paths/cross-platform/matrix/projects/chapter-1/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

content/learning-paths/cross-platform/matrix/projects/chapter-1/.vscode/settings.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

content/learning-paths/cross-platform/matrix/projects/chapter-1/CMakeLists.txt

Lines changed: 0 additions & 23 deletions
This file was deleted.

content/learning-paths/cross-platform/matrix/projects/chapter-1/include/Matrix/Matrix.h

Lines changed: 0 additions & 15 deletions
This file was deleted.

content/learning-paths/cross-platform/matrix/projects/chapter-1/lib/Matrix/Matrix.cpp

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)