Skip to content

Commit 02f9278

Browse files
authored
Merge branch 'main' into Add-Windows-Emscripten-build-of-xeus-cpp-to-ci
2 parents 97a4a56 + dba60ff commit 02f9278

File tree

10 files changed

+141
-22
lines changed

10 files changed

+141
-22
lines changed

.github/workflows/emscripten.yml

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ jobs:
175175
-DLLVM_EXTERNAL_CLING_SOURCE_DIR=../../cling \
176176
-DCMAKE_BUILD_TYPE=Release \
177177
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten \
178-
-DLLVM_ENABLE_ASSERTIONS=ON \
179178
-DLLVM_TARGETS_TO_BUILD="${{ matrix.llvm_targets_to_build }}" \
180179
-DLLVM_ENABLE_LIBEDIT=OFF \
181180
-DLLVM_ENABLE_PROJECTS="${{ matrix.llvm_enable_projects }}" \
@@ -194,6 +193,9 @@ jobs:
194193
-DLLVM_ENABLE_LIBPFM=OFF \
195194
-DCLANG_BUILD_TOOLS=OFF \
196195
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_DIR \
196+
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
197+
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
198+
-DLLVM_ENABLE_LTO=Full \
197199
../llvm
198200
emmake ninja clang cling lld gtest_main
199201
else
@@ -206,7 +208,6 @@ jobs:
206208
cd build
207209
emcmake cmake -DCMAKE_BUILD_TYPE=Release \
208210
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten \
209-
-DLLVM_ENABLE_ASSERTIONS=ON \
210211
-DLLVM_TARGETS_TO_BUILD="${{ matrix.llvm_targets_to_build }}" \
211212
-DLLVM_ENABLE_LIBEDIT=OFF \
212213
-DLLVM_ENABLE_PROJECTS="${{ matrix.llvm_enable_projects }}" \
@@ -225,6 +226,9 @@ jobs:
225226
-DCLANG_BUILD_TOOLS=OFF \
226227
-G Ninja \
227228
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_DIR \
229+
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
230+
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
231+
-DLLVM_ENABLE_LTO=Full \
228232
../llvm
229233
emmake ninja libclang clangInterpreter clangStaticAnalyzerCore lldWasm
230234
fi
@@ -284,7 +288,6 @@ jobs:
284288
-DLLVM_EXTERNAL_CLING_SOURCE_DIR=../../cling `
285289
-DCMAKE_BUILD_TYPE=Release `
286290
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten `
287-
-DLLVM_ENABLE_ASSERTIONS=ON `
288291
-DLLVM_TARGETS_TO_BUILD="${{ matrix.llvm_targets_to_build }}" `
289292
-DLLVM_ENABLE_LIBEDIT=OFF `
290293
-DLLVM_ENABLE_PROJECTS="${{ matrix.llvm_enable_projects }}" `
@@ -302,6 +305,9 @@ jobs:
302305
-DLLVM_BUILD_TOOLS=OFF `
303306
-DLLVM_ENABLE_LIBPFM=OFF `
304307
-DCLANG_BUILD_TOOLS=OFF `
308+
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
309+
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
310+
-DLLVM_ENABLE_LTO=Full `
305311
-DLLVM_NATIVE_TOOL_DIR="$env:NATIVE_DIR" `
306312
..\llvm
307313
emmake make clang cling lld gtest_main
@@ -325,7 +331,6 @@ jobs:
325331
echo "Apply clang${{ matrix.clang-runtime }}-*.patch patches:"
326332
emcmake cmake -DCMAKE_BUILD_TYPE=Release `
327333
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten `
328-
-DLLVM_ENABLE_ASSERTIONS=ON `
329334
-DLLVM_TARGETS_TO_BUILD="${{ matrix.llvm_targets_to_build }}" `
330335
-DLLVM_ENABLE_LIBEDIT=OFF `
331336
-DLLVM_ENABLE_PROJECTS="${{ matrix.llvm_enable_projects }}" `
@@ -342,6 +347,9 @@ jobs:
342347
-DLLVM_BUILD_TOOLS=OFF `
343348
-DLLVM_ENABLE_LIBPFM=OFF `
344349
-DCLANG_BUILD_TOOLS=OFF `
350+
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
351+
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
352+
-DLLVM_ENABLE_LTO=Full `
345353
-G Ninja `
346354
-DLLVM_NATIVE_TOOL_DIR="$env:NATIVE_DIR" `
347355
..\llvm
@@ -527,6 +535,15 @@ jobs:
527535
fi
528536

529537
emmake make -j ${{ env.ncpus }} check-cppinterop
538+
os="${{ matrix.os }}"
539+
if [[ "${os}" != macos* ]] ; then
540+
actual_size=$(stat -c%s "./lib/libclangCppInterOp.so")
541+
max_size=$((40 * 1024 * 1024))
542+
if [[ "$actual_size" -gt "$max_size" ]]; then
543+
echo "Error: libclangCppInterOp.so is larger than 40 MB."
544+
exit 1
545+
fi
546+
fi
530547
cd ./unittests/CppInterOp/
531548
# Fresh install browsers, and run Emscripten tests in them
532549
# This is to match the Emscripten build instructions, where
@@ -689,7 +706,12 @@ jobs:
689706
-DSYSROOT_PATH=$SYSROOT_PATH \
690707
../
691708
fi
692-
emmake make -j ${{ env.ncpus }} check-cppinterop
709+
os="${{ matrix.os }}"
710+
if [[ "${os}" != macos* ]] ; then
711+
EMCC_CORES=1 emmake make -j 1 check-cppinterop
712+
else
713+
EMCC_CORES=2 emmake make -j 2 check-cppinterop
714+
fi
693715
cd ./unittests/CppInterOp/
694716
# Explaination of options for emrun
695717
# --browser (name of browser on path)

Emscripten-build-instructions.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ mkdir build
8080
cd build
8181
emcmake cmake -DCMAKE_BUILD_TYPE=Release \
8282
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten \
83-
-DLLVM_ENABLE_ASSERTIONS=ON \
8483
-DLLVM_TARGETS_TO_BUILD="WebAssembly" \
8584
-DLLVM_ENABLE_LIBEDIT=OFF \
8685
-DLLVM_ENABLE_PROJECTS="clang;lld" \
@@ -98,6 +97,9 @@ emcmake cmake -DCMAKE_BUILD_TYPE=Release \
9897
-DLLVM_ENABLE_LIBPFM=OFF \
9998
-DCLANG_BUILD_TOOLS=OFF \
10099
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_DIR \
100+
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
101+
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
102+
-DLLVM_ENABLE_LTO=Full \
101103
../llvm
102104
emmake make libclang -j $(nproc --all)
103105
emmake make clangInterpreter clangStaticAnalyzerCore -j $(nproc --all)
@@ -118,7 +120,6 @@ mkdir build
118120
cd build
119121
emcmake cmake -DCMAKE_BUILD_TYPE=Release `
120122
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten `
121-
-DLLVM_ENABLE_ASSERTIONS=ON `
122123
-DLLVM_TARGETS_TO_BUILD="WebAssembly" `
123124
-DLLVM_ENABLE_LIBEDIT=OFF `
124125
-DLLVM_ENABLE_PROJECTS="clang;lld" `
@@ -137,6 +138,9 @@ emcmake cmake -DCMAKE_BUILD_TYPE=Release `
137138
-DCLANG_BUILD_TOOLS=OFF `
138139
-DLLVM_NATIVE_TOOL_DIR="$env:NATIVE_DIR" `
139140
-G Ninja `
141+
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
142+
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
143+
-DLLVM_ENABLE_LTO=Full `
140144
..\llvm
141145
emmake ninja libclang clangInterpreter clangStaticAnalyzerCore lldWasm
142146
```

docs/Emscripten-build-instructions.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ and osx
100100
cd build
101101
emcmake cmake -DCMAKE_BUILD_TYPE=Release \
102102
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten \
103-
-DLLVM_ENABLE_ASSERTIONS=ON \
104103
-DLLVM_TARGETS_TO_BUILD="WebAssembly" \
105104
-DLLVM_ENABLE_LIBEDIT=OFF \
106105
-DLLVM_ENABLE_PROJECTS="clang;lld" \
@@ -118,6 +117,9 @@ and osx
118117
-DLLVM_ENABLE_LIBPFM=OFF \
119118
-DCLANG_BUILD_TOOLS=OFF \
120119
-DLLVM_NATIVE_TOOL_DIR=$NATIVE_DIR \
120+
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
121+
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" \
122+
-DLLVM_ENABLE_LTO=Full \
121123
../llvm
122124
emmake make libclang -j $(nproc --all)
123125
emmake make clangInterpreter clangStaticAnalyzerCore -j $(nproc --all)
@@ -138,7 +140,6 @@ or executing
138140
cd build
139141
emcmake cmake -DCMAKE_BUILD_TYPE=Release `
140142
-DLLVM_HOST_TRIPLE=wasm32-unknown-emscripten `
141-
-DLLVM_ENABLE_ASSERTIONS=ON `
142143
-DLLVM_TARGETS_TO_BUILD="WebAssembly" `
143144
-DLLVM_ENABLE_LIBEDIT=OFF `
144145
-DLLVM_ENABLE_PROJECTS="clang;lld" `
@@ -157,6 +158,9 @@ or executing
157158
-DCLANG_BUILD_TOOLS=OFF `
158159
-DLLVM_NATIVE_TOOL_DIR="$env:NATIVE_DIR" `
159160
-G Ninja `
161+
-DCMAKE_C_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
162+
-DCMAKE_CXX_FLAGS_RELEASE="-Oz -g0 -DNDEBUG" `
163+
-DLLVM_ENABLE_LTO=Full `
160164
..\llvm
161165
emmake ninja libclang clangInterpreter clangStaticAnalyzerCore lldWasm
162166

lib/CppInterOp/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,16 @@ if(EMSCRIPTEN)
127127
set_target_properties(clangCppInterOp
128128
PROPERTIES NO_SONAME 1
129129
)
130+
target_compile_options(clangCppInterOp
131+
PRIVATE "SHELL: -Oz"
132+
PRIVATE "SHELL: -flto"
133+
)
130134
target_link_options(clangCppInterOp
131135
PRIVATE "SHELL: -s WASM_BIGINT"
132136
PRIVATE "SHELL: -s SIDE_MODULE=1"
133137
PRIVATE "SHELL: ${SYMBOLS_LIST}"
138+
PRIVATE "SHELL: -Oz"
139+
PRIVATE "SHELL: -flto"
134140
)
135141
else()
136142
target_link_options(clangCppInterOp

lib/CppInterOp/Compatibility.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ inline void codeComplete(std::vector<std::string>& Results,
354354
}
355355

356356
std::vector<std::string> results;
357-
std::vector<std::string> Comps;
358357
clang::CompilerInstance* MainCI = (*Interp)->getCompilerInstance();
359358
auto CC = clang::ReplCodeCompleter();
360359
CC.codeComplete(MainCI, code, complete_line, complete_column,

lib/CppInterOp/CppInterOp.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,6 +1925,7 @@ void get_type_as_string(QualType QT, std::string& type_name, ASTContext& C,
19251925
Policy.SuppressElaboration = true;
19261926
Policy.SuppressTagKeyword = !QT->isEnumeralType();
19271927
Policy.FullyQualifiedName = true;
1928+
Policy.UsePreferredNames = false;
19281929
QT.getAsStringInternal(type_name, Policy);
19291930
}
19301931

@@ -1934,6 +1935,7 @@ static void GetDeclName(const clang::Decl* D, ASTContext& Context,
19341935
PrintingPolicy Policy(Context.getPrintingPolicy());
19351936
Policy.SuppressTagKeyword = true;
19361937
Policy.SuppressUnwrittenScope = true;
1938+
Policy.PrintCanonicalTypes = true;
19371939
if (const TypeDecl* TD = dyn_cast<TypeDecl>(D)) {
19381940
// This is a class, struct, or union member.
19391941
QualType QT;
@@ -1964,9 +1966,24 @@ void collect_type_info(const FunctionDecl* FD, QualType& QT,
19641966
PrintingPolicy Policy(C.getPrintingPolicy());
19651967
Policy.SuppressElaboration = true;
19661968
refType = kNotReference;
1967-
if (QT->isRecordType() && forArgument) {
1968-
get_type_as_string(QT, type_name, C, Policy);
1969-
return;
1969+
if (QT->isRecordType()) {
1970+
if (forArgument) {
1971+
get_type_as_string(QT, type_name, C, Policy);
1972+
return;
1973+
}
1974+
if (auto* CXXRD = QT->getAsCXXRecordDecl()) {
1975+
if (CXXRD->isLambda()) {
1976+
std::string fn_name;
1977+
llvm::raw_string_ostream stream(fn_name);
1978+
Policy.FullyQualifiedName = true;
1979+
Policy.SuppressUnwrittenScope = true;
1980+
FD->getNameForDiagnostic(stream, Policy,
1981+
/*Qualified=*/false);
1982+
type_name = "__internal_CppInterOp::function<decltype(" + fn_name +
1983+
")>::result_type";
1984+
return;
1985+
}
1986+
}
19701987
}
19711988
if (QT->isFunctionPointerType()) {
19721989
std::string fp_typedef_name;
@@ -3221,6 +3238,17 @@ TInterp_t CreateInterpreter(const std::vector<const char*>& Args /*={}*/,
32213238
llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
32223239
}
32233240

3241+
I->declare(R"(
3242+
namespace __internal_CppInterOp {
3243+
template <typename Signature>
3244+
struct function;
3245+
template <typename Res, typename... ArgTypes>
3246+
struct function<Res(ArgTypes...)> {
3247+
typedef Res result_type;
3248+
};
3249+
} // namespace __internal_CppInterOp
3250+
)");
3251+
32243252
sInterpreters->emplace_back(I, /*Owned=*/true);
32253253

32263254
return I;

lib/CppInterOp/exports.ld

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@
5050
-Wl,--export=_ZNK5clang4Type14isFloatingTypeEv
5151
-Wl,--export=_ZNK5clang12FunctionDecl12getNumParamsEv
5252
-Wl,--export=__clang_Interpreter_SetValueNoAlloc
53-
-Wl,--export=__clang_Interpreter_SetValueWithAlloc
53+
-Wl,--export=__clang_Interpreter_SetValueWithAlloc
54+
-Wl,--export=_ZN4llvm15SmallVectorBaseIjE8set_sizeEm

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ TEST(FunctionReflectionTest, GetFunctionRequiredArgs) {
562562
}
563563

564564
TEST(FunctionReflectionTest, GetFunctionArgType) {
565-
std::vector<Decl*> Decls, SubDecls;
565+
std::vector<Decl*> Decls;
566566
std::string code = R"(
567567
void f1(int i, double d, long l, char ch) {}
568568
void f2(const int i, double d[], long *l, char ch[4]) {}
@@ -582,7 +582,7 @@ TEST(FunctionReflectionTest, GetFunctionArgType) {
582582
}
583583

584584
TEST(FunctionReflectionTest, GetFunctionSignature) {
585-
std::vector<Decl*> Decls, SubDecls;
585+
std::vector<Decl*> Decls;
586586
std::string code = R"(
587587
class C {
588588
void f(int i, double d, long l = 0, char ch = 'a') {}
@@ -626,7 +626,8 @@ TEST(FunctionReflectionTest, GetFunctionSignature) {
626626
}
627627

628628
TEST(FunctionReflectionTest, IsTemplatedFunction) {
629-
std::vector<Decl*> Decls, SubDeclsC1, SubDeclsC2;
629+
std::vector<Decl*> Decls;
630+
std::vector<Decl*> SubDeclsC1;
630631
std::string code = R"(
631632
void f1(int a) {}
632633

@@ -1448,7 +1449,7 @@ TEST(FunctionReflectionTest, GetFunctionAddress) {
14481449
#ifdef _WIN32
14491450
GTEST_SKIP() << "Disabled on Windows. Needs fixing.";
14501451
#endif
1451-
std::vector<Decl*> Decls, SubDecls;
1452+
std::vector<Decl*> Decls;
14521453
std::string code = "int f1(int i) { return i * i; }";
14531454
std::vector<const char*> interpreter_args = {"-include", "new"};
14541455

@@ -2111,6 +2112,61 @@ TEST(FunctionReflectionTest, GetFunctionCallWrapper) {
21112112

21122113
auto op_callable = Cpp::MakeFunctionCallable(op);
21132114
EXPECT_EQ(op_callable.getKind(), Cpp::JitCall::kGenericCall);
2115+
2116+
Cpp::Declare(R"(
2117+
enum class MyEnum { A, B, C };
2118+
template <MyEnum E>
2119+
class TemplatedEnum {};
2120+
2121+
namespace MyNameSpace {
2122+
enum class MyEnum { A, B, C };
2123+
template <MyEnum E>
2124+
class TemplatedEnum {};
2125+
}
2126+
)");
2127+
2128+
Cpp::TCppScope_t TemplatedEnum = Cpp::GetScope("TemplatedEnum");
2129+
EXPECT_TRUE(TemplatedEnum);
2130+
2131+
auto TAI_enum =
2132+
Cpp::TemplateArgInfo(Cpp::GetTypeFromScope(Cpp::GetNamed("MyEnum")), "1");
2133+
Cpp::TCppScope_t TemplatedEnum_instantiated =
2134+
Cpp::InstantiateTemplate(TemplatedEnum, &TAI_enum, 1);
2135+
EXPECT_TRUE(TemplatedEnum_instantiated);
2136+
2137+
Cpp::TCppObject_t obj = Cpp::Construct(TemplatedEnum_instantiated);
2138+
EXPECT_TRUE(obj);
2139+
Cpp::Destruct(obj, TemplatedEnum_instantiated);
2140+
obj = nullptr;
2141+
2142+
Cpp::TCppScope_t MyNameSpace_TemplatedEnum =
2143+
Cpp::GetScope("TemplatedEnum", Cpp::GetScope("MyNameSpace"));
2144+
EXPECT_TRUE(TemplatedEnum);
2145+
2146+
TAI_enum = Cpp::TemplateArgInfo(Cpp::GetTypeFromScope(Cpp::GetNamed(
2147+
"MyEnum", Cpp::GetScope("MyNameSpace"))),
2148+
"1");
2149+
Cpp::TCppScope_t MyNameSpace_TemplatedEnum_instantiated =
2150+
Cpp::InstantiateTemplate(MyNameSpace_TemplatedEnum, &TAI_enum, 1);
2151+
EXPECT_TRUE(TemplatedEnum_instantiated);
2152+
2153+
obj = Cpp::Construct(MyNameSpace_TemplatedEnum_instantiated);
2154+
EXPECT_TRUE(obj);
2155+
Cpp::Destruct(obj, MyNameSpace_TemplatedEnum_instantiated);
2156+
obj = nullptr;
2157+
2158+
Cpp::Declare(R"(
2159+
auto get_fn(int x) { return [x](int y){ return x + y; }; }
2160+
)");
2161+
2162+
Cpp::TCppScope_t get_fn = Cpp::GetNamed("get_fn");
2163+
EXPECT_TRUE(get_fn);
2164+
2165+
auto get_fn_callable = Cpp::MakeFunctionCallable(get_fn);
2166+
EXPECT_EQ(get_fn_callable.getKind(), Cpp::JitCall::kGenericCall);
2167+
2168+
EXPECT_TRUE(Cpp::IsLambdaClass(Cpp::GetFunctionReturnType(get_fn)));
2169+
EXPECT_FALSE(Cpp::IsLambdaClass(Cpp::GetFunctionReturnType(bar)));
21142170
}
21152171

21162172
TEST(FunctionReflectionTest, IsConstMethod) {
@@ -2132,7 +2188,7 @@ TEST(FunctionReflectionTest, IsConstMethod) {
21322188
}
21332189

21342190
TEST(FunctionReflectionTest, GetFunctionArgName) {
2135-
std::vector<Decl*> Decls, SubDecls;
2191+
std::vector<Decl*> Decls;
21362192
std::string code = R"(
21372193
void f1(int i, double d, long l, char ch) {}
21382194
void f2(const int i, double d[], long *l, char ch[4]) {}
@@ -2172,7 +2228,7 @@ TEST(FunctionReflectionTest, GetFunctionArgName) {
21722228
}
21732229

21742230
TEST(FunctionReflectionTest, GetFunctionArgDefault) {
2175-
std::vector<Decl*> Decls, SubDecls;
2231+
std::vector<Decl*> Decls;
21762232
std::string code = R"(
21772233
void f1(int i, double d = 4.0, const char *s = "default", char ch = 'c') {}
21782234
void f2(float i = 0.0, double d = 3.123, long m = 34126) {}

unittests/CppInterOp/ScopeReflectionTest.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ TEST(ScopeReflectionTest, IsBuiltin) {
191191
EXPECT_TRUE(Cpp::IsBuiltin(C.getComplexType(C.Float128Ty).getAsOpaquePtr()));
192192

193193
// std::complex
194-
std::vector<Decl*> Decls;
195194
Interp->declare("#include <complex>");
196195
Sema &S = Interp->getCI()->getSema();
197196
auto lookup = S.getStdNamespace()->lookup(&C.Idents.get("complex"));

0 commit comments

Comments
 (0)