Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share unix getexepath() definition via src/native #44999

Merged
merged 12 commits into from
Nov 26, 2020
4 changes: 1 addition & 3 deletions eng/native/build-commons.sh
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ EOF
scan_build=scan-build
fi

engNativeDir="$__RepoRootDir/eng/native"
cmakeArgs="-DCLR_ENG_NATIVE_DIR=\"$engNativeDir\" $cmakeArgs"
nextCommand="\"$engNativeDir/gen-buildsys.sh\" \"$cmakeDir\" \"$tryrunDir\" \"$intermediatesDir\" $platformArch $__Compiler \"$__CompilerMajorVersion\" \"$__CompilerMinorVersion\" $__BuildType \"$generator\" $scan_build $cmakeArgs"
nextCommand="\"$__RepoRootDir/eng/native/gen-buildsys.sh\" \"$cmakeDir\" \"$tryrunDir\" \"$intermediatesDir\" $platformArch $__Compiler \"$__CompilerMajorVersion\" \"$__CompilerMinorVersion\" $__BuildType \"$generator\" $scan_build $cmakeArgs"
echo "Invoking $nextCommand"
eval $nextCommand

Expand Down
7 changes: 7 additions & 0 deletions eng/native/configurepaths.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
get_filename_component(CLR_REPO_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../.. ABSOLUTE)
set(CLR_ENG_NATIVE_DIR ${CMAKE_CURRENT_LIST_DIR})
get_filename_component(CLR_SRC_NATIVE_DIR ${CMAKE_CURRENT_LIST_DIR}/../../src/native ABSOLUTE)

# TO_NATIVE_PATH so it uses backslashes in Windows to avoid getting error,
# as CLR_SRC_NATIVE_DIR is used to specify source files.
file (TO_NATIVE_PATH ${CLR_SRC_NATIVE_DIR} CLR_SRC_NATIVE_DIR)
1 change: 1 addition & 0 deletions src/coreclr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ endif()
# Set the project name
project(CoreCLR)

include(../../eng/native/configurepaths.cmake)
include(${CLR_ENG_NATIVE_DIR}/configurecompiler.cmake)

if(MSVC)
Expand Down
18 changes: 9 additions & 9 deletions src/coreclr/build-runtime.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,10 @@ if %__BuildCrossArchNative% EQU 1 (
set "__CMakeBinDir=!__CMakeBinDir:\=/!"

if %__Ninja% EQU 1 (
set __ExtraCmakeArgs="-DCMAKE_BUILD_TYPE=!__BuildType!"
set __ExtraCmakeArgs="-DCMAKE_BUILD_TYPE=!__BuildType!"
)

set __ExtraCmakeArgs=!__ExtraCmakeArgs! %__CMakeClrBuildSubsetArgs% "-DCLR_CROSS_COMPONENTS_BUILD=1" "-DCLR_CMAKE_TARGET_ARCH=%__BuildArch%" "-DCLR_CMAKE_TARGET_OS=%__TargetOS%" "-DCLR_CMAKE_PGO_INSTRUMENT=0" "-DCLR_CMAKE_OPTDATA_PATH=%__PgoOptDataPath%" "-DCLR_CMAKE_PGO_OPTIMIZE=0" "-DCLR_ENG_NATIVE_DIR=%__RepoRootDir%/eng/native" "-DCLR_REPO_ROOT_DIR=%__RepoRootDir%" %__CMakeArgs%
set __ExtraCmakeArgs=!__ExtraCmakeArgs! %__CMakeClrBuildSubsetArgs% "-DCLR_CROSS_COMPONENTS_BUILD=1" "-DCLR_CMAKE_TARGET_ARCH=%__BuildArch%" "-DCLR_CMAKE_TARGET_OS=%__TargetOS%" "-DCLR_CMAKE_PGO_INSTRUMENT=0" "-DCLR_CMAKE_OPTDATA_PATH=%__PgoOptDataPath%" "-DCLR_CMAKE_PGO_OPTIMIZE=0" %__CMakeArgs%
call "%__RepoRootDir%\eng\native\gen-buildsys.cmd" "%__ProjectDir%" "%__CrossCompIntermediatesDir%" %__VSVersion% %__CrossArch% !__ExtraCmakeArgs!

if not !errorlevel! == 0 (
Expand Down Expand Up @@ -512,22 +512,22 @@ if %__BuildCrossArchNative% EQU 1 (

if /i "%__CrossArch2%" == "x86" ( set __VCBuildArch=x86 )
if /i "%__CrossArch2%" == "x64" ( set __VCBuildArch=x86_amd64 )

echo %__MsgPrefix%Using environment: "%__VCToolsRoot%\vcvarsall.bat" !__VCBuildArch!
call "%__VCToolsRoot%\vcvarsall.bat" !__VCBuildArch!
@if defined _echo @echo on

if not exist "%__CrossComp2IntermediatesDir%" md "%__CrossComp2IntermediatesDir%"
if defined __SkipConfigure goto SkipConfigureCrossBuild2

set __CMakeBinDir="%__CrossComponent2BinDir%"
set "__CMakeBinDir=!__CMakeBinDir:\=/!"

if %__Ninja% EQU 1 (
set __ExtraCmakeArgs="-DCMAKE_BUILD_TYPE=!__BuildType!"
set __ExtraCmakeArgs="-DCMAKE_BUILD_TYPE=!__BuildType!"
)

set __ExtraCmakeArgs=!__ExtraCmakeArgs! %__CMakeClrBuildSubsetArgs% "-DCLR_CROSS_COMPONENTS_BUILD=1" "-DCLR_CMAKE_TARGET_ARCH=%__BuildArch%" "-DCLR_CMAKE_TARGET_OS=%__TargetOS%" "-DCLR_CMAKE_PGO_INSTRUMENT=0" "-DCLR_CMAKE_OPTDATA_PATH=%__PgoOptDataPath%" "-DCLR_CMAKE_PGO_OPTIMIZE=0" "-DCMAKE_SYSTEM_VERSION=10.0" "-DCLR_ENG_NATIVE_DIR=%__RepoRootDir%/eng/native" "-DCLR_REPO_ROOT_DIR=%__RepoRootDir%" %__CMakeArgs%
set __ExtraCmakeArgs=!__ExtraCmakeArgs! %__CMakeClrBuildSubsetArgs% "-DCLR_CROSS_COMPONENTS_BUILD=1" "-DCLR_CMAKE_TARGET_ARCH=%__BuildArch%" "-DCLR_CMAKE_TARGET_OS=%__TargetOS%" "-DCLR_CMAKE_PGO_INSTRUMENT=0" "-DCLR_CMAKE_OPTDATA_PATH=%__PgoOptDataPath%" "-DCLR_CMAKE_PGO_OPTIMIZE=0" "-DCMAKE_SYSTEM_VERSION=10.0" %__CMakeArgs%
call "%__RepoRootDir%\eng\native\gen-buildsys.cmd" "%__ProjectDir%" "%__CrossComp2IntermediatesDir%" %__VSVersion% %__CrossArch2% !__ExtraCmakeArgs!

if not !errorlevel! == 0 (
Expand Down Expand Up @@ -620,10 +620,10 @@ if %__BuildNative% EQU 1 (
echo %__MsgPrefix%Regenerating the Visual Studio solution

if %__Ninja% EQU 1 (
set __ExtraCmakeArgs="-DCMAKE_BUILD_TYPE=!__BuildType!"
set __ExtraCmakeArgs="-DCMAKE_BUILD_TYPE=!__BuildType!"
)

set __ExtraCmakeArgs=!__ExtraCmakeArgs! !___CrossBuildDefine! %__CMakeClrBuildSubsetArgs% "-DCLR_CMAKE_PGO_INSTRUMENT=%__PgoInstrument%" "-DCLR_CMAKE_OPTDATA_PATH=%__PgoOptDataPath%" "-DCLR_CMAKE_PGO_OPTIMIZE=%__PgoOptimize%" "-DCLR_ENG_NATIVE_DIR=%__RepoRootDir%/eng/native" "-DCLR_REPO_ROOT_DIR=%__RepoRootDir%" %__CMakeArgs%
set __ExtraCmakeArgs=!__ExtraCmakeArgs! !___CrossBuildDefine! %__CMakeClrBuildSubsetArgs% "-DCLR_CMAKE_PGO_INSTRUMENT=%__PgoInstrument%" "-DCLR_CMAKE_OPTDATA_PATH=%__PgoOptDataPath%" "-DCLR_CMAKE_PGO_OPTIMIZE=%__PgoOptimize%" %__CMakeArgs%
call "%__RepoRootDir%\eng\native\gen-buildsys.cmd" "%__ProjectDir%" "%__IntermediatesDir%" %__VSVersion% %__BuildArch% !__ExtraCmakeArgs!
if not !errorlevel! == 0 (
echo %__ErrMsgPrefix%%__MsgPrefix%Error: failed to generate native component build project!
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/build-runtime.sh
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ check_prereqs
restore_optdata

# Build the coreclr (native) components.
__CMakeArgs="-DCLR_CMAKE_PGO_INSTRUMENT=$__PgoInstrument -DCLR_CMAKE_OPTDATA_PATH=$__PgoOptDataPath -DCLR_CMAKE_PGO_OPTIMIZE=$__PgoOptimize -DCLR_REPO_ROOT_DIR=\"$__RepoRootDir\" $__CMakeArgs"
__CMakeArgs="-DCLR_CMAKE_PGO_INSTRUMENT=$__PgoInstrument -DCLR_CMAKE_OPTDATA_PATH=$__PgoOptDataPath -DCLR_CMAKE_PGO_OPTIMIZE=$__PgoOptimize $__CMakeArgs"
__CMakeArgs="-DCLR_CMAKE_BUILD_SUBSET_JIT=$__BuildJit -DCLR_CMAKE_BUILD_SUBSET_ALLJITS=$__BuildAllJits -DCLR_CMAKE_BUILD_SUBSET_RUNTIME=$__BuildRuntime $__CMakeArgs"
__CMakeArgs="-DCLR_CMAKE_BUILD_TESTS=$__BuildPALTests $__CMakeArgs"

Expand Down
1 change: 0 additions & 1 deletion src/coreclr/src/hosts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ if(CLR_CMAKE_HOST_WIN32)
add_subdirectory(coreshim)
else(CLR_CMAKE_HOST_WIN32)
add_definitions(-D_FILE_OFFSET_BITS=64)
add_subdirectory(unixcoreruncommon)
add_subdirectory(unixcorerun)
endif(CLR_CMAKE_HOST_WIN32)
2 changes: 2 additions & 0 deletions src/coreclr/src/hosts/corerun/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ set(CoreRun_RESOURCES native.rc)
add_definitions(-DFX_VER_INTERNALNAME_STR=CoreRun.exe)

if(CLR_CMAKE_HOST_UNIX)
include_directories("${CLR_SRC_NATIVE_DIR}/common")

# This does not compile on Linux yet
if(CAN_BE_COMPILED_ON_LINUX)
_add_executable(CoreRun
Expand Down
27 changes: 7 additions & 20 deletions src/coreclr/src/hosts/unixcorerun/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
project(unixcorerun)

include_directories(../unixcoreruncommon)
include_directories("${CLR_SRC_NATIVE_DIR}/common")
include(configure.cmake)

set(CORERUN_SOURCES
corerun.cpp
)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

_add_executable(corerun
${CORERUN_SOURCES}
corerun.cpp
coreruncommon.cpp
am11 marked this conversation as resolved.
Show resolved Hide resolved
)

# FreeBSD and NetBSD implement dlopen(3) in libc
if(NOT CLR_CMAKE_TARGET_FREEBSD AND NOT CLR_CMAKE_TARGET_NETBSD)
target_link_libraries(corerun
dl
)
endif(NOT CLR_CMAKE_TARGET_FREEBSD AND NOT CLR_CMAKE_TARGET_NETBSD)

# Libc turns locks into no-ops if pthread was not loaded into process yet. Loading
# pthread by the process executable ensures that all locks are initialized properly.
target_link_libraries(corerun
unixcoreruncommon
)
target_link_libraries(corerun ${CMAKE_DL_LIBS})

# Android implements pthread natively
if(NOT CLR_CMAKE_TARGET_ANDROID)
target_link_libraries(corerun
pthread
)
target_link_libraries(corerun pthread)
endif()

install_clr(TARGETS corerun)
11 changes: 7 additions & 4 deletions src/coreclr/src/hosts/unixcorerun/corerun.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include <coreruncommon.h>
#include <string>
#include "coreruncommon.h"
#include <getexepath.h>
#include <string.h>
#include <sys/stat.h>

Expand Down Expand Up @@ -125,13 +125,16 @@ int main(const int argc, const char* argv[])
}

// Make sure we have a full path for argv[0].
std::string argv0AbsolutePath;
if (!GetEntrypointExecutableAbsolutePath(argv0AbsolutePath))
char* path = getexepath();
if (!path)
{
perror("Could not get full path");
return -1;
}

std::string argv0AbsolutePath(path);
free(path);

std::string clrFilesAbsolutePath;
if(!GetClrFilesAbsolutePath(argv0AbsolutePath.c_str(), clrFilesPath, clrFilesAbsolutePath))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// Code that is used by both the Unix corerun and coreconsole.
//

#include "config.h"

#include <cstdlib>
#include <cstring>
#include <assert.h>
Expand All @@ -17,16 +15,6 @@
#include <string>
#include <string.h>
#include <sys/stat.h>
#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/param.h>
#endif
#if HAVE_GETAUXVAL
#include <sys/auxv.h>
#endif
#if defined(HAVE_SYS_SYSCTL_H) || defined(__FreeBSD__)
#include <sys/sysctl.h>
#endif
#include "coreruncommon.h"
#include "coreclrhost.h"
#include <unistd.h>
Expand All @@ -50,119 +38,6 @@ static const char* serverGcVar = "COMPlus_gcServer";
// Set to 1 for Globalization Invariant mode to be true. Default is false.
static const char* globalizationInvariantVar = "CORECLR_GLOBAL_INVARIANT";

#if defined(__linux__)
#define symlinkEntrypointExecutable "/proc/self/exe"
#elif !defined(__APPLE__)
#define symlinkEntrypointExecutable "/proc/curproc/exe"
#endif

bool GetEntrypointExecutableAbsolutePath(std::string& entrypointExecutable)
{
bool result = false;

entrypointExecutable.clear();

// Get path to the executable for the current process using
// platform specific means.
#if defined(__APPLE__)

// On Mac, we ask the OS for the absolute path to the entrypoint executable
uint32_t lenActualPath = 0;
if (_NSGetExecutablePath(nullptr, &lenActualPath) == -1)
{
// OSX has placed the actual path length in lenActualPath,
// so re-attempt the operation
std::string resizedPath(lenActualPath, '\0');
char *pResizedPath = const_cast<char *>(resizedPath.c_str());
if (_NSGetExecutablePath(pResizedPath, &lenActualPath) == 0)
{
entrypointExecutable.assign(pResizedPath);
result = true;
}
}
#elif defined (__FreeBSD__)
static const int name[] = {
CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1
};
char path[PATH_MAX];
size_t len;

len = sizeof(path);
if (sysctl(name, 4, path, &len, nullptr, 0) == 0)
{
entrypointExecutable.assign(path);
result = true;
}
else
{
// ENOMEM
result = false;
}
#elif defined(__NetBSD__) && defined(KERN_PROC_PATHNAME)
static const int name[] = {
CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME,
};
char path[MAXPATHLEN];
size_t len;

len = sizeof(path);
if (sysctl(name, __arraycount(name), path, &len, NULL, 0) != -1)
{
entrypointExecutable.assign(path);
result = true;
}
else
{
result = false;
}
#elif defined(__sun)
const char *path;
if ((path = getexecname()) == NULL)
{
result = false;
}
else if (*path != '/')
{
char *cwd;
if ((cwd = getcwd(NULL, PATH_MAX)) == NULL)
{
result = false;
}
else
{
entrypointExecutable
.assign(cwd)
.append("/")
.append(path);
result = true;
free(cwd);
}
}
else
{
entrypointExecutable.assign(path);
result = true;
}
#else

#if HAVE_GETAUXVAL && defined(AT_EXECFN)
const char *execfn = (const char *)getauxval(AT_EXECFN);

if (execfn)
{
entrypointExecutable.assign(execfn);
result = true;
}
else
#endif
// On other OSs, return the symlink that will be resolved by GetAbsolutePath
// to fetch the entrypoint EXE absolute path, inclusive of filename.
result = GetAbsolutePath(symlinkEntrypointExecutable, entrypointExecutable);
#endif

return result;
}

bool GetAbsolutePath(const char* path, std::string& absolutePath)
{
bool result = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include "config.h"
#include <string>

// Get the path to entrypoint executable
bool GetEntrypointExecutableAbsolutePath(std::string& entrypointExecutable);

// Get absolute path from the specified path.
// Return true in case of a success, false otherwise.
bool GetAbsolutePath(const char* path, std::string& absolutePath);
Expand Down
14 changes: 0 additions & 14 deletions src/coreclr/src/hosts/unixcoreruncommon/CMakeLists.txt

This file was deleted.

2 changes: 2 additions & 0 deletions src/coreclr/src/pal/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
add_compile_options(-Wa,--divide)
endif()

include_directories("${CLR_SRC_NATIVE_DIR}/common")

set(SOURCES
cruntime/file.cpp
cruntime/filecrt.cpp
Expand Down
7 changes: 4 additions & 3 deletions src/coreclr/src/pal/src/config.h.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _PAL_CONFIG_H_INCLUDED
#define _PAL_CONFIG_H_INCLUDED 1
#ifndef PAL_CONFIG_H_INCLUDED
#define PAL_CONFIG_H_INCLUDED

#cmakedefine01 HAVE_VM_FLAGS_SUPERPAGE_SIZE_ANY
#cmakedefine01 HAVE_MAP_HUGETLB
Expand Down Expand Up @@ -170,4 +170,5 @@
#define SIGWAIT_FAILS_WHEN_PASSED_FULL_SIGSET 0
#define WRITE_0_BYTES_HANGS_TTY 0
#define HAVE_FTRUNCATE_LARGE_LENGTH_SUPPORT 1
#endif

#endif // PAL_CONFIG_H_INCLUDED
Loading