GoogleTest hello world.

./build and ./test work automatically when cwd is inside userland/libs/XXX
without --package-all.
This commit is contained in:
Ciro Santilli 六四事件 法轮功
2020-11-25 00:00:00 +00:00
parent 50570326d4
commit 39073519b1
11 changed files with 156 additions and 32 deletions

9
.gitmodules vendored
View File

@@ -11,6 +11,9 @@
[submodule "submodules/crosstool-ng"]
path = submodules/crosstool-ng
url = https://github.com/cirosantilli/crosstool-ng
[submodule "submodules/freebsd"]
path = submodules/freebsd
url = https://github.com/cirosantilli/freebsd
[submodule "submodules/gcc"]
path = submodules/gcc
url = https://github.com/cirosantilli/gcc
@@ -30,6 +33,9 @@
# git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
# But git clone --branch --depth 1 worked weirdly:
# https://unix.stackexchange.com/questions/338578/linux-kernel-source-code-size-difference
[submodule "submodules/googletest"]
path = submodules/googletest
url = https://github.com/cirosantilli/googletest
[submodule "submodules/linux"]
path = submodules/linux
url = https://github.com/cirosantilli/linux
@@ -39,6 +45,3 @@
[submodule "submodules/xen"]
path = submodules/xen
url = https://github.com/cirosantilli/xen
[submodule "submodules/freebsd"]
path = submodules/freebsd
url = https://github.com/cirosantilli/freebsd

View File

@@ -13504,7 +13504,7 @@ Running individual unit tests is not yet exposed, but it is easy to do: while ru
so you can just copy paste the command.
Building individual tests is possible with:
Building individual tests is possible with `--unit-test` (singular, no 's'):
....
./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
* https://stackoverflow.com/questions/3582608/how-to-correctly-implement-custom-iterators-and-const-iterators
* https://stackoverflow.com/questions/6471019/can-should-i-inherit-from-an-stl-iterator
[[cpp-third-party-libraries]]
==== C++ third-party libraries
Under: <<userland-libs-directory>>.
===== Boost
link:https://++en.wikipedia.org/wiki/Boost_(C%2B%2B_libraries)++[]
link:userland/libs/boost[]:
* link:userland/libs/boost/bimap.cpp[]
===== GoogleTest
https://github.com/google/googletest
On Ubuntu 20.04, the package:
....
sudo apt install googletest
....
does not contain prebuilts, and it is intentional, it is incomprehensible:
* https://askubuntu.com/questions/97626/how-to-install-googletest/1295185#1295185
* https://askubuntu.com/questions/145887/why-no-library-files-installed-for-google-test
so you might as well just `git clone` and build the damned thing yourself:
....
git submodule update --init submodules/googletest
cd submodules/googletest
mkdir build
cd build
cmake ..
make -j`nproc`
cd ../../userland/libs/googletest
./build
....
link:userland/libs/googletest[]:
* userland/libs/googletest/main.cpp[]
===== HDF5
https://en.wikipedia.org/wiki/Hierarchical_Data_Format
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
Examples:
* link:userland/libs/hdf5[]
* gem5 can dump statistics as HDF5: <<gem5-hdf5-statistics>>
=== POSIX
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
./build-userland --package-all
....
==== Boost
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.:
link:https://++en.wikipedia.org/wiki/Boost_(C%2B%2B_libraries)++[]
....
sudo apt install libeigen3-dev
cd userland/libs/eigen
./build
....
link:userland/libs/boost[]
then that library will be automatically enabled.
* link:userland/libs/boost/bimap.cpp[]
See also:
==== HDF5
https://en.wikipedia.org/wiki/Hierarchical_Data_Format
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
Examples:
* link:userland/libs/hdf5[]
* gem5 can dump statistics as HDF5: <<gem5-hdf5-statistics>>
* <<cpp-third-party-libraries>>
=== Userland content filename conventions

View File

@@ -10,16 +10,19 @@ build_userland = lkmc.import_path.import_path_relative_root('build-userland')
class Main(build_userland.Main):
def __init__(self):
super().__init__(
description='''\
https://cirosantilli.com/linux-kernel-module-cheat#userland-setup-getting-started-natively
''',
defaults = {
'archs': [platform.processor()],
'gcc_which': 'host',
'in_tree': True,
'targets': ['.'],
}
if self.cwd_in_lib():
defaults['package_all'] = True
super().__init__(
description='''\
https://cirosantilli.com/linux-kernel-module-cheat#userland-setup-getting-started-natively
''',
defaults=defaults
)
if __name__ == '__main__':

View File

@@ -69,6 +69,8 @@ consts['kernel_modules_subdir'] = 'kernel_modules'
consts['kernel_modules_source_dir'] = os.path.join(consts['root_dir'], consts['kernel_modules_subdir'])
consts['userland_subdir'] = 'userland'
consts['userland_source_dir'] = os.path.join(consts['root_dir'], consts['userland_subdir'])
consts['userland_libs_basename'] = 'libs'
consts['userland_source_libs_dir'] = os.path.join(consts['userland_source_dir'], consts['userland_libs_basename'])
consts['userland_source_arch_dir'] = os.path.join(consts['userland_source_dir'], 'arch')
consts['userland_executable_ext'] = '.out'
consts['baremetal_executable_ext'] = '.elf'
@@ -83,6 +85,7 @@ consts['crosstool_ng_supported_archs'] = set(['arm', 'aarch64'])
consts['linux_source_dir'] = os.path.join(consts['submodules_dir'], 'linux')
consts['linux_config_dir'] = os.path.join(consts['root_dir'], 'linux_config')
consts['gem5_default_source_dir'] = os.path.join(consts['submodules_dir'], 'gem5')
consts['googletest_source_dir'] = os.path.join(consts['submodules_dir'], 'googletest')
consts['rootfs_overlay_dir'] = os.path.join(consts['root_dir'], 'rootfs_overlay')
consts['extract_vmlinux'] = os.path.join(consts['linux_source_dir'], 'scripts', 'extract-vmlinux')
consts['qemu_source_dir'] = os.path.join(consts['submodules_dir'], 'qemu')
@@ -1358,6 +1361,10 @@ lunch aosp_{}-eng
'''
return os.path.join(env['gem5_executable_dir'], name + env['gem5_executable_suffix'])
@staticmethod
def cwd_in_lib():
return pathlib.Path(common.consts['userland_source_libs_dir']) in pathlib.Path(os.getcwd()).parents
def gem5_list_checkpoint_dirs(self):
'''
List checkpoint directory, oldest first.
@@ -1896,14 +1903,15 @@ after configure, e.g. SCons. Usually contains specific targets or other build fl
'-march={}'.format(self.env['march']), LF,
])
if dirpath_relative_root_components_len > 0:
if dirpath_relative_root_components[0] == 'userland':
if dirpath_relative_root_components[0] == consts['userland_subdir']:
if dirpath_relative_root_components_len > 1:
if dirpath_relative_root_components[1] == 'libs':
if dirpath_relative_root_components[1] == self.env['userland_libs_basename']:
if dirpath_relative_root_components_len > 1:
if self.env['gcc_which'] == 'host':
eigen_root = '/'
else:
eigen_root = self.env['buildroot_staging_dir']
# TODO move to path_properties.py somehow.
packages = {
'boost': {
# Header only, no pkg-config package.
@@ -1931,6 +1939,17 @@ after configure, e.g. SCons. Usually contains specific targets or other build fl
'hdf5': {
'pkg_config_id': 'hdf5-serial',
},
'googletest': {
'cc_flags': [
'-I', os.path.join(self.env['googletest_source_dir'], 'googletest', 'include'), LF,
'-I', os.path.join(self.env['googletest_source_dir'], 'googlemock', 'include'), LF,
],
'cc_flags_after': [
os.path.join(self.env['googletest_source_dir'], 'build', 'lib', 'libgtest.a'), LF,
os.path.join(self.env['googletest_source_dir'], 'build', 'lib', 'libgtest_main.a'), LF,
os.path.join(self.env['googletest_source_dir'], 'build', 'lib', 'libgmock.a'), LF,
],
},
}
package_key = dirpath_relative_root_components[2]
if package_key in packages:
@@ -1951,7 +1970,7 @@ after configure, e.g. SCons. Usually contains specific targets or other build fl
]).decode()
cc_flags.extend(self.sh.shlex_split(pkg_config_output))
if 'cc_flags_after' in package:
cc_flags.extend(package['cc_flags_after'])
cc_flags_after.extend(package['cc_flags_after'])
else:
pkg_config_output = subprocess.check_output([
self.env['pkg_config'],

View File

@@ -749,6 +749,12 @@ path_properties_tuples = (
{'requires_dynamic_library': True},
{
'cython': {'no_build': True},
'googletest': (
{},
{
'fail.cpp': {'exit_status': 1},
}
),
'libdrm': {'requires_sudo': True},
'hdf5': (
{},

1
submodules/googletest Submodule

Submodule submodules/googletest added at b1fbd33c06

View File

@@ -6,15 +6,18 @@ test_user_mode = lkmc.import_path.import_path_relative_root('test-executables')
class Main(test_user_mode.Main):
def __init__(self):
super().__init__(
description='''\
https://cirosantilli.com/linux-kernel-module-cheat#userland-setup-getting-started-natively
''',
defaults = {
'emulators': ['native'],
'in_tree': True,
'tests': ['.'],
}
if self.cwd_in_lib():
defaults['package_all'] = True
super().__init__(
description='''\
https://cirosantilli.com/linux-kernel-module-cheat#userland-setup-getting-started-natively
''',
defaults=defaults
)
if __name__ == '__main__':

View File

@@ -0,0 +1 @@
../build

View File

@@ -0,0 +1,9 @@
#include <gtest/gtest.h>
TEST(FailTest, ThisTestPasses) {
EXPECT_EQ(1, 1);
}
TEST(FailTest, ThisTestFails) {
EXPECT_EQ(1, 2);
}

View File

@@ -0,0 +1,27 @@
#include <utility>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using testing::ElementsAre;
using testing::Pair;
TEST(MainTest, MyTest0) {
EXPECT_EQ(1, 1);
// Array comparison.
// https://stackoverflow.com/questions/1460703/comparison-of-arrays-in-google-test
ASSERT_THAT(
(std::vector<int>{5, 10, 15}),
ElementsAre(5, 10, 15)
);
ASSERT_THAT(
(std::vector<std::pair<int, int>>{{1, -1}, {2, -2}}),
ElementsAre(Pair(1, -1), Pair(2, -2))
);
}
TEST(MainTest, MyTest1) {
EXPECT_EQ(1, 1);
}

View File

@@ -0,0 +1 @@
../test