Skip to content

Commit

Permalink
[MERGE #2490 @dilijev] Add -v and --version flags to ch to print Chak…
Browse files Browse the repository at this point in the history
…raCore version.

Merge pull request #2490 from dilijev:version-r14

Partially addresses #109 and #2472.

This is intended as a first pass at the issue to provide some information at this point.

Later, we plan to add more information such as whether a binary was produced as a developer build or an official build, what specific commit the binary was built from, which branch, which machine, etc. The specific set of information that we plan to provide is still being discussed in #109.

```
ch /v
ch /version
ch -v
ch --v
ch -version
ch --version
```

(Supporting `-` and `--` prefixes for short and long flag names is consistent with how we process all behaviors. The logic I used here is a replica of that in `CmdLineArgsParser::Parse`.)

The above all display the version information for `ch` and `ChakraCore` as `<major>.<minor>.<patch>.<QFE>` (note: by convention, ChakraCore's `QFE` is always `0`).

For example on Windows for ChakraCore 1.4.1 with `ch.exe` and `ChakraCore.dll` from the same build:

```
$ ch -v
ch.exe version 1.4.1.0
chakracore.dll version 1.4.1.0
```

If with the same `ch.exe`, if `ChakraCore.dll` was from, e.g., a 2.0.0 build:

```
$ ch -v
ch.exe version 1.4.1.0
chakracore.dll version 2.0.0.0
```

On Linux:

```
~/dev/ChakraCore$ ./BuildLinux/Release/ch -v
ch version 1.4.1.0
```

This flag is available in all flavors (including Release).

---

Updating the help message. Also enable `-h` and `-help` as per request in #2472.

Enables `-h` and `-help` for non-release builds, and `-?`, `-h`, and `-help` for release builds.

Messages will look as follows.

debug/test builds:

```
Usage: ch.exe [-v|-version] [-h|-help] [-?] [flaglist] <source file>
        -v|-version             Displays version info
        -h|-help                Displays this help message
        -?                      Displays this help message with complete [flaglist] info
```

release builds:

```
Usage: ch.exe [-v|-version] [-h|-help|-?] <source file>
Note: [flaglist] is not supported in Release builds; try a Debug or Test build to enable these flags.
        -v|-version             Displays version info
        -h|-help|-?             Displays this help message
```

The distinction is that `-?` is already implemented in debug/test builds to display full information about the `[flaglist]` options. This is a minimal change here that will result in guiding the user more accurately in all build flavors, the effect of which is that `-?` in debug/test builds is still the only way to get information about the `[flaglist]` options.
  • Loading branch information
dilijev committed Feb 9, 2017
2 parents b5c277f + beb522d commit a7203fc
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 7 deletions.
5 changes: 5 additions & 0 deletions bin/ch/ChakraRtInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ ChakraRTInterface::ArgInfo* ChakraRTInterface::m_argInfo = nullptr;
TestHooks ChakraRTInterface::m_testHooks = { 0 };
JsAPIHooks ChakraRTInterface::m_jsApiHooks = { 0 };

LPCSTR GetChakraDllName()
{
return chakraDllName;
}

// Wrapper functions to abstract out loading ChakraCore
// and resolving its symbols
// Currently, these functions resolve to the PAL on Linux
Expand Down
2 changes: 2 additions & 0 deletions bin/ch/ChakraRtInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ struct JsAPIHooks
JsrtTTDReplayExecutionPtr pfJsrtTTDReplayExecution;
};

LPCSTR GetChakraDllName();

class ChakraRTInterface
{
public:
Expand Down
2 changes: 1 addition & 1 deletion bin/ch/HostConfigFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void HostConfigFlags::PrintUsage()
pfnPrintUsage();
}

wprintf(_u("\nFlag List : \n"));
wprintf(_u("\nHost Config Flags: \n\n"));
HostConfigFlags::PrintUsageString();
ChakraRTInterface::PrintConfigFlagsUsageString();
}
Expand Down
124 changes: 119 additions & 5 deletions bin/ch/ch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
//-------------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include "Core/AtomLockGuids.h"
#include <CommonPal.h>
#ifdef _WIN32
#include <winver.h>
#include <process.h>
#endif

Expand Down Expand Up @@ -64,17 +66,88 @@ int HostExceptionFilter(int exceptionCode, _EXCEPTION_POINTERS *ep)

void __stdcall PrintUsageFormat()
{
wprintf(_u("\nUsage: %s [flaglist] <source file>\n"), hostName);
wprintf(_u("\nUsage: %s [-v|-version] [-h|-help] [-?] [flaglist] <source file>\n"), hostName);
wprintf(_u("\t-v|-version\t\tDisplays version info\n"));
wprintf(_u("\t-h|-help\t\tDisplays this help message\n"));
wprintf(_u("\t-?\t\t\tDisplays this help message with complete [flaglist] info\n"));
}

#if !defined(ENABLE_DEBUG_CONFIG_OPTIONS)
void __stdcall PrintReleaseUsage()
{
wprintf(_u("\nUsage: %s [-v|-version] [-h|-help|-?] <source file> %s"), hostName,
_u("\nNote: [flaglist] is not supported in Release builds; try a Debug or Test build to enable these flags.\n"));
wprintf(_u("\t-v|-version\t\tDisplays version info\n"));
wprintf(_u("\t-h|-help|-?\t\tDisplays this help message\n"));
}
#endif

void __stdcall PrintUsage()
{
#if !defined(ENABLE_DEBUG_CONFIG_OPTIONS)
wprintf(_u("\nUsage: %s <source file> %s"), hostName,
_u("\n[flaglist] is not supported for Release mode\n"));
PrintReleaseUsage();
#else
PrintUsageFormat();
wprintf(_u("Try '%s -?' for help\n"), hostName);
#endif
}

void __stdcall PrintChVersion()
{
wprintf(_u("%s version %d.%d.%d.0\n"), hostName, CHAKRA_CORE_MAJOR_VERSION, CHAKRA_CORE_MINOR_VERSION, CHAKRA_CORE_PATCH_VERSION);
}

#ifdef _WIN32
void __stdcall PrintChakraCoreVersion()
{
char filename[_MAX_PATH];
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];

LPCSTR chakraDllName = GetChakraDllName();

char modulename[_MAX_PATH];
GetModuleFileNameA(NULL, modulename, _MAX_PATH);
_splitpath_s(modulename, drive, _MAX_DRIVE, dir, _MAX_DIR, nullptr, 0, nullptr, 0);
_makepath_s(filename, drive, dir, chakraDllName, nullptr);

UINT size = 0;
LPBYTE lpBuffer = NULL;
DWORD verSize = GetFileVersionInfoSizeA(filename, NULL);

if (verSize != NULL)
{
LPSTR verData = new char[verSize];

if (GetFileVersionInfoA(filename, NULL, verSize, verData) &&
VerQueryValue(verData, _u("\\"), (VOID FAR * FAR *)&lpBuffer, &size) &&
(size != 0))
{
VS_FIXEDFILEINFO *verInfo = (VS_FIXEDFILEINFO *)lpBuffer;
if (verInfo->dwSignature == VS_FFI_SIGNATURE)
{
// Doesn't matter if you are on 32 bit or 64 bit,
// DWORD is always 32 bits, so first two revision numbers
// come from dwFileVersionMS, last two come from dwFileVersionLS
printf("%s version %d.%d.%d.%d\n",
chakraDllName,
(verInfo->dwFileVersionMS >> 16) & 0xffff,
(verInfo->dwFileVersionMS >> 0) & 0xffff,
(verInfo->dwFileVersionLS >> 16) & 0xffff,
(verInfo->dwFileVersionLS >> 0) & 0xffff);
}
}

delete[] verData;
}
}
#endif

void __stdcall PrintVersion()
{
PrintChVersion();

#ifdef _WIN32
PrintChakraCoreVersion();
#endif
}

Expand Down Expand Up @@ -770,7 +843,48 @@ int _cdecl wmain(int argc, __in_ecount(argc) LPWSTR argv[])
int cpos = 1;
for(int i = 1; i < argc; ++i)
{
if(wcsstr(argv[i], _u("-TTRecord=")) == argv[i])
const wchar *arg = argv[i];
size_t arglen = wcslen(arg);

// support - or / prefix for flags
if (arglen >= 1 && (arg[0] == _u('-')
#ifdef _WIN32
|| arg[0] == _u('/') // '/' prefix for legacy (Windows-only because it starts a path on Unix)
#endif
))
{
// support -- prefix for flags
if (arglen >= 2 && arg[0] == _u('-') && arg[1] == _u('-'))
{
arg += 2; // advance past -- prefix
}
else
{
arg += 1; // advance past - or / prefix
}
}

arglen = wcslen(arg); // get length of flag after prefix
if ((arglen == 1 && wcsncmp(arg, _u("v"), arglen) == 0) ||
(arglen == 7 && wcsncmp(arg, _u("version"), arglen) == 0))
{
PrintVersion();
PAL_Shutdown();
return EXIT_SUCCESS;
}
else if (
#if !defined(ENABLE_DEBUG_CONFIG_OPTIONS) // release builds can display some kind of help message
(arglen == 1 && wcsncmp(arg, _u("?"), arglen) == 0) ||
#endif
(arglen == 1 && wcsncmp(arg, _u("h"), arglen) == 0) ||
(arglen == 4 && wcsncmp(arg, _u("help"), arglen) == 0)
)
{
PrintUsage();
PAL_Shutdown();
return EXIT_SUCCESS;
}
else if(wcsstr(argv[i], _u("-TTRecord=")) == argv[i])
{
doTTRecord = true;
wchar* ruri = argv[i] + wcslen(_u("-TTRecord="));
Expand Down
3 changes: 2 additions & 1 deletion bin/ch/ch.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
ole32.lib;
kernel32.lib;
Rpcrt4.lib;
version.lib;
</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
Expand Down Expand Up @@ -102,4 +103,4 @@
</ItemGroup>
<Import Project="$(BuildConfigPropsPath)Chakra.Build.targets" Condition="exists('$(BuildConfigPropsPath)Chakra.Build.targets')" />
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
</Project>
</Project>

0 comments on commit a7203fc

Please sign in to comment.