Skip to content

Commit 3907351

Browse files
committed
GoogleTest hello world.
./build and ./test work automatically when cwd is inside userland/libs/XXX without --package-all.
1 parent 5057032 commit 3907351

File tree

11 files changed

+156
-32
lines changed

11 files changed

+156
-32
lines changed

.gitmodules

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
[submodule "submodules/crosstool-ng"]
1212
path = submodules/crosstool-ng
1313
url = https://github.com/cirosantilli/crosstool-ng
14+
[submodule "submodules/freebsd"]
15+
path = submodules/freebsd
16+
url = https://github.com/cirosantilli/freebsd
1417
[submodule "submodules/gcc"]
1518
path = submodules/gcc
1619
url = https://github.com/cirosantilli/gcc
@@ -30,6 +33,9 @@
3033
# git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
3134
# But git clone --branch --depth 1 worked weirdly:
3235
# https://unix.stackexchange.com/questions/338578/linux-kernel-source-code-size-difference
36+
[submodule "submodules/googletest"]
37+
path = submodules/googletest
38+
url = https://github.com/cirosantilli/googletest
3339
[submodule "submodules/linux"]
3440
path = submodules/linux
3541
url = https://github.com/cirosantilli/linux
@@ -39,6 +45,3 @@
3945
[submodule "submodules/xen"]
4046
path = submodules/xen
4147
url = https://github.com/cirosantilli/xen
42-
[submodule "submodules/freebsd"]
43-
path = submodules/freebsd
44-
url = https://github.com/cirosantilli/freebsd

README.adoc

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13504,7 +13504,7 @@ Running individual unit tests is not yet exposed, but it is easy to do: while ru
1350413504

1350513505
so you can just copy paste the command.
1350613506

13507-
Building individual tests is possible with:
13507+
Building individual tests is possible with `--unit-test` (singular, no 's'):
1350813508

1350913509
....
1351013510
./build-gem5 --unit-test base/circlebuf.test
@@ -20628,6 +20628,62 @@ link:userland/cpp/custom_iterator.cpp[]: there is no way to easily define a nice
2062820628
* https://stackoverflow.com/questions/3582608/how-to-correctly-implement-custom-iterators-and-const-iterators
2062920629
* https://stackoverflow.com/questions/6471019/can-should-i-inherit-from-an-stl-iterator
2063020630

20631+
[[cpp-third-party-libraries]]
20632+
==== C++ third-party libraries
20633+
20634+
Under: <<userland-libs-directory>>.
20635+
20636+
===== Boost
20637+
20638+
link:https://++en.wikipedia.org/wiki/Boost_(C%2B%2B_libraries)++[]
20639+
20640+
link:userland/libs/boost[]:
20641+
20642+
* link:userland/libs/boost/bimap.cpp[]
20643+
20644+
===== GoogleTest
20645+
20646+
https://github.com/google/googletest
20647+
20648+
On Ubuntu 20.04, the package:
20649+
20650+
....
20651+
sudo apt install googletest
20652+
....
20653+
20654+
does not contain prebuilts, and it is intentional, it is incomprehensible:
20655+
20656+
* https://askubuntu.com/questions/97626/how-to-install-googletest/1295185#1295185
20657+
* https://askubuntu.com/questions/145887/why-no-library-files-installed-for-google-test
20658+
20659+
so you might as well just `git clone` and build the damned thing yourself:
20660+
20661+
....
20662+
git submodule update --init submodules/googletest
20663+
cd submodules/googletest
20664+
mkdir build
20665+
cd build
20666+
cmake ..
20667+
make -j`nproc`
20668+
cd ../../userland/libs/googletest
20669+
./build
20670+
....
20671+
20672+
link:userland/libs/googletest[]:
20673+
20674+
* userland/libs/googletest/main.cpp[]
20675+
20676+
===== HDF5
20677+
20678+
https://en.wikipedia.org/wiki/Hierarchical_Data_Format
20679+
20680+
Binary format to store data. TODO vs databases, notably SQLite: https://datascience.stackexchange.com/questions/262/hierarchical-data-format-what-are-the-advantages-compared-to-alternative-format
20681+
20682+
Examples:
20683+
20684+
* link:userland/libs/hdf5[]
20685+
* gem5 can dump statistics as HDF5: <<gem5-hdf5-statistics>>
20686+
2063120687
=== POSIX
2063220688

2063320689
Programs under link:userland/posix/[] are examples of POSIX C programming.
@@ -21344,24 +21400,19 @@ See for example <<blas>>. Since it is located under `userland/libs/openblas`, it
2134421400
./build-userland --package-all
2134521401
....
2134621402

21347-
==== Boost
21348-
21349-
link:https://++en.wikipedia.org/wiki/Boost_(C%2B%2B_libraries)++[]
21350-
21351-
link:userland/libs/boost[]
21403+
As an exception, if you first `cd` directly into one of the directories and do a <<userland-setup-getting-started-natively,native host build>>, e.g.:
2135221404

21353-
* link:userland/libs/boost/bimap.cpp[]
21354-
21355-
==== HDF5
21356-
21357-
https://en.wikipedia.org/wiki/Hierarchical_Data_Format
21405+
....
21406+
sudo apt install libeigen3-dev
21407+
cd userland/libs/eigen
21408+
./build
21409+
....
2135821410

21359-
Binary format to store data. TODO vs databases, notably SQLite: https://datascience.stackexchange.com/questions/262/hierarchical-data-format-what-are-the-advantages-compared-to-alternative-format
21411+
then that library will be automatically enabled.
2136021412

21361-
Examples:
21413+
See also:
2136221414

21363-
* link:userland/libs/hdf5[]
21364-
* gem5 can dump statistics as HDF5: <<gem5-hdf5-statistics>>
21415+
* <<cpp-third-party-libraries>>
2136521416

2136621417
=== Userland content filename conventions
2136721418

build-userland-in-tree

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ build_userland = lkmc.import_path.import_path_relative_root('build-userland')
1010

1111
class Main(build_userland.Main):
1212
def __init__(self):
13+
defaults = {
14+
'archs': [platform.processor()],
15+
'gcc_which': 'host',
16+
'in_tree': True,
17+
'targets': ['.'],
18+
}
19+
if self.cwd_in_lib():
20+
defaults['package_all'] = True
1321
super().__init__(
1422
description='''\
1523
https://cirosantilli.com/linux-kernel-module-cheat#userland-setup-getting-started-natively
1624
''',
17-
defaults={
18-
'archs': [platform.processor()],
19-
'gcc_which': 'host',
20-
'in_tree': True,
21-
'targets': ['.'],
22-
}
25+
defaults=defaults
2326
)
2427

2528
if __name__ == '__main__':

common.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
consts['kernel_modules_source_dir'] = os.path.join(consts['root_dir'], consts['kernel_modules_subdir'])
7070
consts['userland_subdir'] = 'userland'
7171
consts['userland_source_dir'] = os.path.join(consts['root_dir'], consts['userland_subdir'])
72+
consts['userland_libs_basename'] = 'libs'
73+
consts['userland_source_libs_dir'] = os.path.join(consts['userland_source_dir'], consts['userland_libs_basename'])
7274
consts['userland_source_arch_dir'] = os.path.join(consts['userland_source_dir'], 'arch')
7375
consts['userland_executable_ext'] = '.out'
7476
consts['baremetal_executable_ext'] = '.elf'
@@ -83,6 +85,7 @@
8385
consts['linux_source_dir'] = os.path.join(consts['submodules_dir'], 'linux')
8486
consts['linux_config_dir'] = os.path.join(consts['root_dir'], 'linux_config')
8587
consts['gem5_default_source_dir'] = os.path.join(consts['submodules_dir'], 'gem5')
88+
consts['googletest_source_dir'] = os.path.join(consts['submodules_dir'], 'googletest')
8689
consts['rootfs_overlay_dir'] = os.path.join(consts['root_dir'], 'rootfs_overlay')
8790
consts['extract_vmlinux'] = os.path.join(consts['linux_source_dir'], 'scripts', 'extract-vmlinux')
8891
consts['qemu_source_dir'] = os.path.join(consts['submodules_dir'], 'qemu')
@@ -1358,6 +1361,10 @@ def get_gem5_target_path(env, name):
13581361
'''
13591362
return os.path.join(env['gem5_executable_dir'], name + env['gem5_executable_suffix'])
13601363

1364+
@staticmethod
1365+
def cwd_in_lib():
1366+
return pathlib.Path(common.consts['userland_source_libs_dir']) in pathlib.Path(os.getcwd()).parents
1367+
13611368
def gem5_list_checkpoint_dirs(self):
13621369
'''
13631370
List checkpoint directory, oldest first.
@@ -1896,14 +1903,15 @@ def _build_one(
18961903
'-march={}'.format(self.env['march']), LF,
18971904
])
18981905
if dirpath_relative_root_components_len > 0:
1899-
if dirpath_relative_root_components[0] == 'userland':
1906+
if dirpath_relative_root_components[0] == consts['userland_subdir']:
19001907
if dirpath_relative_root_components_len > 1:
1901-
if dirpath_relative_root_components[1] == 'libs':
1908+
if dirpath_relative_root_components[1] == self.env['userland_libs_basename']:
19021909
if dirpath_relative_root_components_len > 1:
19031910
if self.env['gcc_which'] == 'host':
19041911
eigen_root = '/'
19051912
else:
19061913
eigen_root = self.env['buildroot_staging_dir']
1914+
# TODO move to path_properties.py somehow.
19071915
packages = {
19081916
'boost': {
19091917
# Header only, no pkg-config package.
@@ -1931,6 +1939,17 @@ def _build_one(
19311939
'hdf5': {
19321940
'pkg_config_id': 'hdf5-serial',
19331941
},
1942+
'googletest': {
1943+
'cc_flags': [
1944+
'-I', os.path.join(self.env['googletest_source_dir'], 'googletest', 'include'), LF,
1945+
'-I', os.path.join(self.env['googletest_source_dir'], 'googlemock', 'include'), LF,
1946+
],
1947+
'cc_flags_after': [
1948+
os.path.join(self.env['googletest_source_dir'], 'build', 'lib', 'libgtest.a'), LF,
1949+
os.path.join(self.env['googletest_source_dir'], 'build', 'lib', 'libgtest_main.a'), LF,
1950+
os.path.join(self.env['googletest_source_dir'], 'build', 'lib', 'libgmock.a'), LF,
1951+
],
1952+
},
19341953
}
19351954
package_key = dirpath_relative_root_components[2]
19361955
if package_key in packages:
@@ -1951,7 +1970,7 @@ def _build_one(
19511970
]).decode()
19521971
cc_flags.extend(self.sh.shlex_split(pkg_config_output))
19531972
if 'cc_flags_after' in package:
1954-
cc_flags.extend(package['cc_flags_after'])
1973+
cc_flags_after.extend(package['cc_flags_after'])
19551974
else:
19561975
pkg_config_output = subprocess.check_output([
19571976
self.env['pkg_config'],

path_properties.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,12 @@ def get(path):
749749
{'requires_dynamic_library': True},
750750
{
751751
'cython': {'no_build': True},
752+
'googletest': (
753+
{},
754+
{
755+
'fail.cpp': {'exit_status': 1},
756+
}
757+
),
752758
'libdrm': {'requires_sudo': True},
753759
'hdf5': (
754760
{},

submodules/googletest

Submodule googletest added at b1fbd33

test-executables-in-tree

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ test_user_mode = lkmc.import_path.import_path_relative_root('test-executables')
66

77
class Main(test_user_mode.Main):
88
def __init__(self):
9+
defaults = {
10+
'emulators': ['native'],
11+
'in_tree': True,
12+
'tests': ['.'],
13+
}
14+
if self.cwd_in_lib():
15+
defaults['package_all'] = True
916
super().__init__(
1017
description='''\
1118
https://cirosantilli.com/linux-kernel-module-cheat#userland-setup-getting-started-natively
1219
''',
13-
defaults={
14-
'emulators': ['native'],
15-
'in_tree': True,
16-
'tests': ['.'],
17-
}
20+
defaults=defaults
1821
)
1922

2023
if __name__ == '__main__':

userland/libs/googletest/build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../build

userland/libs/googletest/fail.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <gtest/gtest.h>
2+
3+
TEST(FailTest, ThisTestPasses) {
4+
EXPECT_EQ(1, 1);
5+
}
6+
7+
TEST(FailTest, ThisTestFails) {
8+
EXPECT_EQ(1, 2);
9+
}

userland/libs/googletest/main.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <utility>
2+
#include <vector>
3+
4+
#include <gmock/gmock.h>
5+
#include <gtest/gtest.h>
6+
7+
using testing::ElementsAre;
8+
using testing::Pair;
9+
10+
TEST(MainTest, MyTest0) {
11+
EXPECT_EQ(1, 1);
12+
13+
// Array comparison.
14+
// https://stackoverflow.com/questions/1460703/comparison-of-arrays-in-google-test
15+
ASSERT_THAT(
16+
(std::vector<int>{5, 10, 15}),
17+
ElementsAre(5, 10, 15)
18+
);
19+
ASSERT_THAT(
20+
(std::vector<std::pair<int, int>>{{1, -1}, {2, -2}}),
21+
ElementsAre(Pair(1, -1), Pair(2, -2))
22+
);
23+
}
24+
25+
TEST(MainTest, MyTest1) {
26+
EXPECT_EQ(1, 1);
27+
}

0 commit comments

Comments
 (0)