Skip to content

Commit 4dc5c0c

Browse files
dpotmaneboasson
authored andcommitted
Add initial set of idlc tests
This adds a set of tests for idlc. These tests run the whole process of compiling an idl file, compile the output with a C compiler and run a test program that writes and reads data and compares the result. Signed-off-by: Dennis Potman <[email protected]>
1 parent e14f170 commit 4dc5c0c

11 files changed

+609
-0
lines changed

.azure/templates/build-test.yml

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ steps:
111111
-DENABLE_COVERAGE=${COVERAGE:-off} \
112112
-DENABLE_SHM=${ICEORYX:-off} \
113113
-DBUILD_TESTING=on \
114+
-DBUILD_IDLC_TESTING=${IDLC_TESTING:-on} \
114115
-DBUILD_EXAMPLES=on \
115116
-DWERROR=on \
116117
${GENERATOR:+-G} "${GENERATOR}" -A "${PLATFORM}" -T "${TOOLSET}" ..

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ script:
264264
-DENABLE_TOPIC_DISCOVERY=${TOPIC_DISCOVERY}
265265
-DENABLE_COVERAGE=${CODECOV}
266266
-DBUILD_TESTING=on
267+
-DBUILD_IDLC_TESTING=on
267268
-DBUILD_EXAMPLES=on
268269
-DWERROR=on
269270
-G "${GENERATOR}" ..

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ set(MEMORYCHECK_COMMAND_OPTIONS "--track-origins=yes --leak-check=full --trace-c
243243
# build the product itself, switch to off by default.
244244
option(BUILD_TESTING "Build the testing tree." OFF)
245245

246+
# Include the tests for idlc. These tests use the idl compiler (C back-end) to
247+
# compile an idl file at (test) runtime, and use the C compiler to build a test
248+
# application for the generated types, that is executed to do the actual testing.
249+
option(BUILD_IDLC_TESTING "Build the idl compiler tests" OFF)
250+
246251
# Disable building examples by default because it is not strictly required.
247252
option(BUILD_EXAMPLES "Build examples." OFF)
248253

azure-pipelines.yml

+4
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,22 @@ strategy:
7878
arch: x86
7979
image: windows-2019
8080
conanfile: conanfile102.txt
81+
idlc_testing: off
8182
generator: 'Visual Studio 16 2019'
8283
toolset: v141
8384
'Windows 2019 with Visual Studio 2019 (Debug, x86_64)':
8485
image: windows-2019
86+
idlc_testing: off
8587
generator: 'Visual Studio 16 2019'
8688
'Windows 2019 with Visual Studio 2019 (Release, x86_64)':
8789
image: windows-2019
8890
build_type: Release
91+
idlc_testing: off
8992
generator: 'Visual Studio 16 2019'
9093
'Windows 2019 with GCC 10 (Debug, x86_64)':
9194
image: windows-2019
9295
build_type: Debug
96+
idlc_testing: off
9397
generator: 'MinGW Makefiles'
9498
cc: 'C:/msys64/mingw64/bin/gcc.exe'
9599
cxx: 'C:/msys64/mingw64/bin/g++.exe'

src/tools/idlc/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,7 @@ install(
9595
include("${CycloneDDS_SOURCE_DIR}/cmake/Modules/Generate.cmake")
9696

9797
add_subdirectory(tests)
98+
99+
if(BUILD_IDLC_TESTING)
100+
add_subdirectory(xtests)
101+
endif()

src/tools/idlc/xtests/CMakeLists.txt

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#
2+
# Copyright(c) 2006 to 2021 ADLINK Technology Limited and others
3+
#
4+
# This program and the accompanying materials are made available under the
5+
# terms of the Eclipse Public License v. 2.0 which is available at
6+
# http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
7+
# v. 1.0 which is available at
8+
# http://www.eclipse.org/org/documents/edl-v10.php.
9+
#
10+
# SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
#
12+
13+
set(_sources
14+
test_basic.idl
15+
test_union.idl
16+
test_alias.idl)
17+
18+
set(_includes "${_includes}\\;${CMAKE_SOURCE_DIR}/src/core/ddsc/include")
19+
set(_includes "${_includes}\\;${CMAKE_SOURCE_DIR}/src/core/ddsi/include")
20+
set(_includes "${_includes}\\;${CMAKE_SOURCE_DIR}/src/ddsrt/include")
21+
set(_includes "${_includes}\\;${CMAKE_BINARY_DIR}/src/core/include")
22+
23+
set(CMAKE_VERBOSE_MAKEFILE ON)
24+
25+
if(MSVC)
26+
if(CMAKE_CL_64)
27+
if(MSVC_VERSION GREATER 1599)
28+
# VS 10 and later:
29+
set(CMAKE_MSVC_ARCH x64)
30+
else()
31+
# VS 9 and earlier:
32+
set(CMAKE_MSVC_ARCH amd64)
33+
endif()
34+
else()
35+
set(CMAKE_MSVC_ARCH x86)
36+
endif()
37+
38+
# Add the include dir from the Windows Kits directory
39+
# code from https://gitlab.kitware.com/cmake/cmake/-/blob/v3.17.3/Modules/InstallRequiredSystemLibraries.cmake
40+
get_filename_component(windows_kits_dir
41+
"[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots;KitsRoot10]" ABSOLUTE)
42+
set(programfilesx86 "ProgramFiles(x86)")
43+
if(";${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION};$ENV{UCRTVersion};$ENV{WindowsSDKVersion};" MATCHES [=[;(10\.[0-9.]+)[;\]]=])
44+
set(__ucrt_version "${CMAKE_MATCH_1}/")
45+
else()
46+
set(__ucrt_version "")
47+
endif()
48+
find_path(WINDOWS_KITS_DIR
49+
NAMES
50+
Redist/${__ucrt_version}ucrt/DLLs/${CMAKE_MSVC_ARCH}/ucrtbase.dll
51+
Redist/ucrt/DLLs/${CMAKE_MSVC_ARCH}/ucrtbase.dll
52+
PATHS
53+
$ENV{CMAKE_WINDOWS_KITS_10_DIR}
54+
"${windows_kits_dir}"
55+
"$ENV{ProgramFiles}/Windows Kits/10"
56+
"$ENV{${programfilesx86}}/Windows Kits/10"
57+
)
58+
set(_windows_kit_includes "${WINDOWS_KITS_DIR}/include/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
59+
set(_windows_kit_libs "${WINDOWS_KITS_DIR}/lib/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
60+
61+
# get msvc root
62+
get_filename_component(_msvc_dir ${CMAKE_C_COMPILER} DIRECTORY)
63+
set(_msvc_dir "${_msvc_dir}/../../..")
64+
65+
# Add include directory VC/Tools/MSVC/[version]/include
66+
set(_includes "${_includes}\\;${_msvc_dir}/include")
67+
set(_includes "${_includes}\\;${_windows_kit_includes}/ucrt")
68+
set(_includes "${_includes}\\;${_windows_kit_includes}/um")
69+
set(_includes "${_includes}\\;${_windows_kit_includes}/shared")
70+
71+
# Add library paths
72+
set(_libs "${_msvc_dir}/lib/${CMAKE_MSVC_ARCH}")
73+
set(_libs "${_libs}\\;${_windows_kit_libs}/ucrt/${CMAKE_MSVC_ARCH}")
74+
set(_libs "${_libs}\\;${_windows_kit_libs}/um/${CMAKE_MSVC_ARCH}")
75+
endif()
76+
77+
78+
79+
set(_dir "${CMAKE_CURRENT_SOURCE_DIR}")
80+
foreach(_source ${_sources})
81+
set(_source "${_dir}/${_source}")
82+
get_filename_component(_base "${_source}" NAME_WE)
83+
add_test(
84+
NAME "idlc_${_base}"
85+
COMMAND ${CMAKE_COMMAND} -P "${_dir}/Compile.cmake" ${_source})
86+
set_property(
87+
TEST "idlc_${_base}"
88+
APPEND PROPERTY ENVIRONMENT
89+
"CC=${CMAKE_C_COMPILER}"
90+
"IDLC=$<TARGET_FILE:CycloneDDS::idlc>"
91+
"MAINC=${CMAKE_CURRENT_SOURCE_DIR}/main.c"
92+
"TEST_INCLUDE_PATHS=${_includes}"
93+
"TEST_LIB_PATHS=${_libs}"
94+
"CDDS_LIB_PATH=$<TARGET_LINKER_FILE_DIR:CycloneDDS::ddsc>"
95+
"CDDS_BIN_PATH=$<TARGET_FILE_DIR:CycloneDDS::ddsc>"
96+
"SANITIZER=${SANITIZER}")
97+
if(APPLE)
98+
set_property(
99+
TEST "idlc_${_base}"
100+
APPEND PROPERTY ENVIRONMENT
101+
"OSX_SYSROOT=${CMAKE_OSX_SYSROOT}")
102+
endif()
103+
endforeach()

src/tools/idlc/xtests/Compile.cmake

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#
2+
# Copyright(c) 2021 ADLINK Technology Limited and others
3+
#
4+
# This program and the accompanying materials are made available under the
5+
# terms of the Eclipse Public License v. 2.0 which is available at
6+
# http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
7+
# v. 1.0 which is available at
8+
# http://www.eclipse.org/org/documents/edl-v10.php.
9+
#
10+
# SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
#
12+
set(_c_compiler "$ENV{CC}")
13+
set(_idl_compiler "$ENV{IDLC}")
14+
15+
if(NOT _c_compiler)
16+
message(FATAL_ERROR "C compiler not set")
17+
endif()
18+
if(NOT _idl_compiler)
19+
message(FATAL_ERROR "IDL compiler not set")
20+
endif()
21+
22+
if(WIN32)
23+
set(_separator ";")
24+
else()
25+
set(_separator ":")
26+
endif()
27+
28+
foreach(_path $ENV{TEST_INCLUDE_PATHS})
29+
if(_include_paths)
30+
set(_include_paths "${_include_paths}${_separator}${_path}")
31+
else()
32+
set(_include_paths "${_path}")
33+
endif()
34+
endforeach()
35+
36+
# set compiler specific values
37+
if(_c_compiler MATCHES "cl\\.exe$")
38+
set(ENV{INCLUDE} "${_include_paths}")
39+
set(ENV{PATH} "$ENV{PATH};$ENV{CDDS_BIN_PATH}")
40+
set(_output_flag "/Fe")
41+
set(_lib_path "/link /LIBPATH:\"$ENV{CDDS_LIB_PATH}\"")
42+
foreach(_path $ENV{TEST_LIB_PATHS})
43+
set(_lib_path "${_lib_path} /LIBPATH:\"${_path}\"")
44+
endforeach()
45+
set(_lib "ddsc.lib")
46+
set(_dbg "/Zi")
47+
else()
48+
set(ENV{C_INCLUDE_PATH} "${_include_paths}")
49+
set(ENV{LD_LIBRARY_PATH} "$ENV{LD_LIBRARY_PATH}:$ENV{CDDS_LIB_PATH}")
50+
set(ENV{LIBRARY_PATH} "$ENV{LIBRARY_PATH}:$ENV{CDDS_LIB_PATH}")
51+
set(_output_flag "-o")
52+
set(_lib_path "")
53+
set(_lib "-lddsc")
54+
set(_fdbg "-g")
55+
if(APPLE)
56+
set(_fsysroot "-isysroot$ENV{OSX_SYSROOT}") # no space after isysroot because option in placed in quotes
57+
endif()
58+
foreach(san $ENV{SANITIZER})
59+
if(san STREQUAL "address")
60+
set(_fnofp "-fno-omit-frame-pointer")
61+
endif()
62+
if(san AND NOT san STREQUAL "none")
63+
set(_fsan "-fsanitize=${san}")
64+
endif()
65+
endforeach()
66+
endif()
67+
68+
# get list of sources from argument list
69+
set(_skip_next FALSE)
70+
foreach(_argno RANGE 1 ${CMAKE_ARGC}) # skip executable
71+
if(_skip_next)
72+
set(_skip_next FALSE)
73+
continue()
74+
endif()
75+
76+
set(_arg "${CMAKE_ARGV${_argno}}")
77+
if(_arg MATCHES "^-P")
78+
set(_skip_next TRUE)
79+
else()
80+
list(APPEND _sources "${_arg}")
81+
endif()
82+
endforeach()
83+
if(NOT _sources)
84+
message(FATAL_ERROR "no source files specified")
85+
endif()
86+
87+
foreach(_source ${_sources})
88+
get_filename_component(_base ${_source} NAME_WE)
89+
set(_main $ENV{MAINC})
90+
set(_base_dir ${CMAKE_CURRENT_BINARY_DIR}/${_base})
91+
set(_type ${_base_dir}/${_base}.fn.c)
92+
set(_c ${_base_dir}/${_base}.c)
93+
file(MAKE_DIRECTORY ${_base_dir})
94+
configure_file(${_source} ${_type})
95+
96+
# idl compile the idl file
97+
execute_process(
98+
COMMAND ${_idl_compiler} ${_source}
99+
COMMAND_ECHO STDOUT
100+
WORKING_DIRECTORY ${_base_dir}
101+
RESULT_VARIABLE _result)
102+
if(NOT _result EQUAL "0")
103+
message(FATAL_ERROR "Cannot transpile ${_source} to source code")
104+
endif()
105+
106+
# compile and link c files
107+
execute_process(
108+
COMMAND ${_c_compiler} ${_fdbg} ${_fnofp} ${_fsan} ${_fsysroot} ${_output_flag}${_base} ${_main} ${_type} ${_c} ${_lib} ${_lib_path}
109+
COMMAND_ECHO STDOUT
110+
WORKING_DIRECTORY ${_base_dir}
111+
RESULT_VARIABLE _result)
112+
if(NOT _result EQUAL "0")
113+
message(FATAL_ERROR "Cannot compile ${_source}")
114+
endif()
115+
116+
# execute test process
117+
execute_process(
118+
COMMAND ${_base_dir}/${_base}
119+
COMMAND_ECHO STDOUT
120+
RESULT_VARIABLE _result
121+
ERROR_VARIABLE _error)
122+
if(NOT _result EQUAL "0")
123+
message(FATAL_ERROR "Test failed ${_source}: ${_result} ${_error}")
124+
endif()
125+
126+
endforeach()

src/tools/idlc/xtests/main.c

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright(c) 2021 ADLINK Technology Limited and others
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
7+
* v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
#include <string.h>
16+
#include "dds/ddsrt/heap.h"
17+
#include "dds/ddsi/ddsi_serdata.h"
18+
#include "dds/ddsi/ddsi_cdrstream.h"
19+
#include "dds/ddsc/dds_opcodes.h"
20+
21+
extern dds_topic_descriptor_t *desc;
22+
extern void init_sample (void *s);
23+
extern int cmp_sample (const void *sa, const void *sb);
24+
25+
static void free_sample (void *s)
26+
{
27+
dds_stream_free_sample (s, desc->m_ops);
28+
dds_free (s);
29+
}
30+
31+
int main(int argc, char **argv)
32+
{
33+
(void)argc;
34+
(void)argv;
35+
36+
printf("Running test for type %s\n", desc->m_typename);
37+
38+
// init data
39+
void *msg_wr = ddsrt_calloc (1, desc->m_size);
40+
init_sample(msg_wr);
41+
42+
// create sertype
43+
struct ddsi_sertype_default sertype;
44+
memset (&sertype, 0, sizeof (sertype));
45+
sertype.type = (struct ddsi_sertype_default_desc) {
46+
.size = desc->m_size,
47+
.align = desc->m_align,
48+
.flagset = desc->m_flagset,
49+
.keys.nkeys = 0,
50+
.keys.keys = NULL,
51+
.ops.nops = dds_stream_countops (desc->m_ops, desc->m_nkeys, desc->m_keys),
52+
.ops.ops = (uint32_t *) desc->m_ops
53+
};
54+
55+
// write data
56+
dds_ostream_t os;
57+
os.m_buffer = NULL;
58+
os.m_index = 0;
59+
os.m_size = 0;
60+
os.m_xcdr_version = CDR_ENC_VERSION_2;
61+
dds_stream_write_sample (&os, msg_wr, &sertype);
62+
printf("cdr write complete\n");
63+
64+
// output raw cdr
65+
// for (uint32_t n = 0; n < os.m_index; n++)
66+
// {
67+
// printf("%02x ", os.m_buffer[n]);
68+
// if (!((n + 1) % 16))
69+
// printf("\n");
70+
// }
71+
// printf("\n");
72+
73+
// read data
74+
dds_istream_t is;
75+
is.m_buffer = os.m_buffer;
76+
is.m_index = 0;
77+
is.m_size = os.m_size;
78+
is.m_xcdr_version = CDR_ENC_VERSION_2;
79+
80+
void *msg_rd = ddsrt_calloc (1, desc->m_size);
81+
dds_stream_read_sample (&is, msg_rd, &sertype);
82+
printf("cdr read complete\n");
83+
84+
/* Check for expected result */
85+
int res = cmp_sample(msg_wr, msg_rd);
86+
printf("compare result: %d\n", res);
87+
88+
dds_ostream_fini (&os);
89+
// is->_buffer aliases os->_buffer, so no free
90+
free_sample(msg_wr);
91+
free_sample(msg_rd);
92+
return res;
93+
}

0 commit comments

Comments
 (0)