Skip to content

Commit

Permalink
[clangd] Add library for clangd main function
Browse files Browse the repository at this point in the history
The diff adds a library and header for clangd main function. That change allows to create custom builds for clangd outside the main LLVM repo. The diff also allows to use build system different from CMake to build clangd. The main reason for such change is an ability to use custom clang-tidy modules (created outside LLVM repo).

Test Plan:
```
ninja clangd
```
also check that necessary libs are installed aka
```
ninja install
...
ls <install folder>/lib/libclangdMain.a
```

Differential Revision: https://reviews.llvm.org/D145302
  • Loading branch information
ivanmurashko committed Jul 11, 2023
1 parent 6daa4b9 commit 56ac9d4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
21 changes: 14 additions & 7 deletions clang-tools-extra/clangd/tool/CMakeLists.txt
@@ -1,6 +1,13 @@
add_clang_tool(clangd
# Needed by LLVM's CMake checks because this file defines multiple targets.
set(LLVM_OPTIONAL_SOURCES ClangdToolMain.cpp)

add_clang_library(clangdMain
ClangdMain.cpp
Check.cpp
)

add_clang_tool(clangd
ClangdToolMain.cpp
$<TARGET_OBJECTS:obj.clangDaemonTweaks>
)

Expand All @@ -13,7 +20,7 @@ if(CLANGD_BUILD_XPC)
list(APPEND CLANGD_XPC_LIBS "clangdXpcJsonConversions" "clangdXpcTransport")
endif()

clang_target_link_libraries(clangd
clang_target_link_libraries(clangdMain
PRIVATE
clangAST
clangBasic
Expand All @@ -25,14 +32,14 @@ clang_target_link_libraries(clangd
clangToolingCore
clangToolingRefactoring
clangToolingSyntax
)

target_link_libraries(clangd
PRIVATE
clangTidy

clangDaemon
clangdRemoteIndex
clangdSupport
${CLANGD_XPC_LIBS}
)

clang_target_link_libraries(clangd
PRIVATE
clangdMain
)
11 changes: 5 additions & 6 deletions clang-tools-extra/clangd/tool/ClangdMain.cpp
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "ClangdMain.h"
#include "ClangdLSPServer.h"
#include "CodeComplete.h"
#include "Compiler.h"
Expand Down Expand Up @@ -710,19 +711,14 @@ class FlagsConfigProvider : public config::Provider {
}
};
} // namespace
} // namespace clangd
} // namespace clang

enum class ErrorResultCode : int {
NoShutdownRequest = 1,
CantRunAsXPCService = 2,
CheckFailed = 3
};

int main(int argc, char *argv[]) {
using namespace clang;
using namespace clang::clangd;

int clangdMain(int argc, char *argv[]) {
llvm::InitializeAllTargetInfos();
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
llvm::sys::AddSignalHandler(
Expand Down Expand Up @@ -1041,3 +1037,6 @@ clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment var

return ExitCode;
}

} // namespace clangd
} // namespace clang
19 changes: 19 additions & 0 deletions clang-tools-extra/clangd/tool/ClangdMain.h
@@ -0,0 +1,19 @@
//===--- ClangdMain.h - clangd main function ------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_TOOL_CLANGDMAIN_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_TOOL_CLANGDMAIN_H

namespace clang {
namespace clangd {
// clangd main function (clangd server loop)
int clangdMain(int argc, char *argv[]);
} // namespace clangd
} // namespace clang

#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_TOOL_CLANGDMAIN_H
13 changes: 13 additions & 0 deletions clang-tools-extra/clangd/tool/ClangdToolMain.cpp
@@ -0,0 +1,13 @@
//===--- ClangdToolMain.cpp - clangd main function ------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "ClangdMain.h"

int main(int argc, char **argv) {
return clang::clangd::clangdMain(argc, argv);
}

0 comments on commit 56ac9d4

Please sign in to comment.