1# Copyright (C) 2002-2021 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with GCC; see the file COPYING3.  If not see
15# <http://www.gnu.org/licenses/>.
16#
17# This file was written by Jakub Jelinek, <jakub@redhat.com>
18# Based on compat.exp writte by Janis Johnson, <janis187@us.ibm.com>
19
20
21# Test interoperability of two compilers that follow the same ABI.
22#
23# Break simple tests into two pieces and see that they work when linked
24# together.  If an alternate compiler is specified then the two main
25# pieces of each test are compiled with different compilers.  The
26# alternate compiler must be installed, and is specified by defining
27# ALT_CXX_UNDER_TEST in the environment.
28#
29# struct-layout-1 are generated structure layout interoperability tests,
30# so a generator first needs to be compiled on host, run there and the
31# generated tests then compiled on build and executed on target.
32
33if $tracelevel then {
34    strace $tracelevel
35}
36
37global GXX_UNDER_TEST
38global ld_library_path
39
40# Load procedures from common libraries.
41load_lib standard.exp
42load_lib g++.exp
43load_lib target-libpath.exp
44
45#
46# compat-use-alt-compiler -- make the alternate compiler the default
47#
48proc compat-use-alt-compiler { } {
49    global GXX_UNDER_TEST ALT_CXX_UNDER_TEST
50    global CXXFLAGS
51    global ALWAYS_CXXFLAGS
52    global ld_library_path alt_ld_library_path
53    global same_alt
54
55    # We don't need to do this if the alternate compiler is actually
56    # the same as the compiler under test.
57    if { $same_alt == 0 } then {
58	set GXX_UNDER_TEST $ALT_CXX_UNDER_TEST
59	set CXXFLAGS ""
60	set ALWAYS_CXXFLAGS ""
61	set ld_library_path $alt_ld_library_path
62	set_ld_library_path_env_vars
63	restore_gcc_exec_prefix_env_var
64	clear_effective_target_cache
65    }
66}
67
68#
69# compat-use-tst-compiler -- make compiler under test the default
70#
71proc compat-use-tst-compiler { } {
72    global GXX_UNDER_TEST save_gxx_under_test
73    global CXXFLAGS save_cxxflags
74    global ALWAYS_CXXFLAGS save_always_cxxflags
75    global ld_library_path save_ld_library_path
76    global same_alt
77
78    # We don't need to do this if the alternate compiler is actually
79    # the same as the compiler under test.
80
81    if { $same_alt == 0 } then {
82	set GXX_UNDER_TEST $save_gxx_under_test
83	set CXXFLAGS $save_cxxflags
84	set ALWAYS_CXXFLAGS $save_always_cxxflags
85	set ld_library_path $save_ld_library_path
86	set_ld_library_path_env_vars
87    }
88}
89
90# Load the language-independent compabibility support procedures.
91# This must be done after the compat-use-*-compiler definitions.
92load_lib compat.exp
93
94# Provide the g++-dg-prune routine (gcc-dp.exp is loaded by compat.exp)
95load_lib g++-dg.exp
96
97g++_init
98
99# Save variables for the C++ compiler under test, which each test will
100# change a couple of times.  This must be done after calling g++-init.
101set save_gxx_under_test $GXX_UNDER_TEST
102set save_cxxflags $CXXFLAGS
103set save_always_cxxflags $ALWAYS_CXXFLAGS
104set save_ld_library_path $ld_library_path
105
106# Define an identifier for use with this suite to avoid name conflicts
107# with other compat tests running at the same time.
108set sid "cp_compat"
109
110# Find out whether there is an alternate compiler to test.  If the
111# variable is defined but is set to "same", that means we use the same
112# compiler twice, which is meaningful if the two parts of COMPAT_OPTIONS
113# are different.
114set use_alt 0
115set same_alt 0
116set alt_ld_library_path "."
117if [info exists ALT_CXX_UNDER_TEST] then {
118    set use_alt 1
119    if [string match "same" $ALT_CXX_UNDER_TEST] then {
120	set same_alt 1
121    } else {
122	if [info exists ALT_LD_LIBRARY_PATH] then {
123	    append alt_ld_library_path ":${ALT_LD_LIBRARY_PATH}"
124	}
125    }
126}
127
128set tstobjdir "$tmpdir/g++.dg-struct-layout-1"
129set generator "$tmpdir/g++.dg-struct-layout-1_generate.exe"
130
131set generator_src "$srcdir/$subdir/struct-layout-1_generate.c"
132set generator_src "$generator_src $srcdir/$subdir/../../gcc.dg/compat/generate-random.c"
133set generator_src "$generator_src $srcdir/$subdir/../../gcc.dg/compat/generate-random_r.c"
134set generator_cmd "-o $generator $generator_src"
135
136# Temporarily switch to the environment of the host compiler.
137restore_ld_library_path_env_vars
138set status [remote_exec build "$HOSTCC $HOSTCFLAGS $generator_cmd"]
139set status [lindex $status 0]
140set_ld_library_path_env_vars
141if { $status == 0 } then {
142    file delete -force $tstobjdir
143    file mkdir $tstobjdir
144    set generator_args "-s $srcdir/$subdir -d $tstobjdir"
145    set test_cxx14_vs_cxx17 1
146    if { $use_alt != 0 } then {
147	compat-use-alt-compiler
148	if { [check_no_compiler_messages_nocache compat_alt_has_cxx14 object {
149		int dummy; } "-std=c++14"] == 0 } {
150	    set test_cxx14_vs_cxx17 0
151	} elseif { [check_no_compiler_messages_nocache compat_alt_has_cxx17 object {
152		int dummy; } "-std=c++17"] == 0 } {
153	    set test_cxx14_vs_cxx17 0
154	}
155	compat-use-tst-compiler
156    }
157    if { $test_cxx14_vs_cxx17 != 0 } then {
158	set generator_args "$generator_args -c"
159    }
160    if [info exists env(RUN_ALL_COMPAT_TESTS) ] then {
161	set generator_args "$generator_args -n 15000"
162    }
163    if [check_effective_target_short_enums] then {
164	set generator_args "-e $generator_args"
165    }
166    set status [remote_exec host "$generator $generator_args"]
167    set status [lindex $status 0]
168    if { $status == 0 } then {
169	foreach src [lsort [find $tstobjdir *_main.C]] {
170	    # If we're only testing specific files and this isn't one
171	    # of them, skip it.
172	    if ![runtest_file_p $runtests $src] then {
173		continue
174	    }
175
176	    compat-execute $src $sid $use_alt
177	}
178    } else {
179	warning "Could not execute g++.dg/compat/struct-layout-1 generator"
180    }
181} else {
182    warning "Could not compile g++.dg/compat/struct-layout-1 generator"
183}
184
185# Restore the original compiler under test.
186compat-use-tst-compiler
187