Skip to content

Commit

Permalink
fixes for windows compilation (defining NOMINMAX)
Browse files Browse the repository at this point in the history
installation-instructions for Windows/Visual Studio
  • Loading branch information
Tobias Loew committed Jan 8, 2022
1 parent bb39faf commit bc7ef88
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Testing/Temporary
CPackConfig.cmake
CPackSourceConfig.cmake
_CPack_Packages
/CMakeSettings.json

## libraries
*.a
Expand All @@ -68,6 +69,7 @@ src/lfortran/parser/tokenizer.cpp
src/lfortran/parser/preprocessor.cpp
src/lfortran/ast.h
src/lfortran/asr.h
src/libasr/asr.h
src/lfortran/config.h
share/jupyter/kernels/fortran/kernel.json
src/runtime/*.o.empty.c
Expand Down Expand Up @@ -141,6 +143,7 @@ MANIFEST

## Editor Files
.vscode/
.vs/

## Build Files
*/bin/lfortran
Expand Down
32 changes: 32 additions & 0 deletions doc/src/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,38 @@ Run an interactive prompt:
./src/bin/lfortran
```

## Build on Windows with Visual Studio

Install Conda for example by installing the Miniconda installation by following instructions there for your platform. If not already done, activate the Conda-Installation (cf. Conda installation instructions).

First, clone the repo to a local folder.

Launch a Conda command interpreter and run the following commands:
```bash
conda update -q conda
conda install -c conda-forge python=3.7 re2c m2-bison xonsh llvmdev=11.1.0 jupyter xeus=1.0.1 xtl nlohmann_json cppzmq jupyter_kernel_test pytest
```
Next, from the Conda prompt, launch an `xonsh`-shell:
```bash
xonsh
```
and `cd`` to the root of the repository and run
```bash
bash ci/version.sh
python grammar/asdl_cpp.py
python grammar/asdl_cpp.py grammar/ASR.asdl src/libasr/asr.h
pushd src/lfortran/parser && re2c -W -b tokenizer.re -o tokenizer.cpp && popd
pushd src/lfortran/parser && re2c -W -b preprocessor.re -o preprocessor.cpp && popd
pushd src/lfortran/parser && bison -Wall -d -r all parser.yy && popd
```

Now, you can launch Visual Studio and open the LFortran folder.
Before the first build you have to set up the `ZLib`-pathes: Go to the CMake-Settings (Project -> CMake Setttings for lfortran) and check `Show advanced variables`. Scroll to the `ZLIB_...` variables and set:
- `ZLIB_INCLUDE_DIR` = \<Conda-Installation-Path\>\Library\include
- `ZLIB_LIBRARY_[DEBUG|RELEASE]` = \<Conda-Installation-Path\>\Library\lib\zlib.lib

Then you can generate the CMake-Cache and build the project.

## Enabling the Jupyter Kernel

To install the Jupyter kernel, install the following Conda packages also:
Expand Down
3 changes: 3 additions & 0 deletions src/bin/tpl/cpp-terminal/terminal_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include <stdexcept>

#ifdef _WIN32
# ifndef NOMINMAX
# define NOMINMAX
# endif // NOMINMAX
# include <conio.h>
# define _WINSOCKAPI_
# include <windows.h>
Expand Down
4 changes: 3 additions & 1 deletion src/bin/tpl/whereami/whereami.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ extern "C" {
#endif

#if defined(_WIN32)

#ifndef NOMINMAX
#define NOMINMAX
#endif // NOMINMAX
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
Expand Down
77 changes: 77 additions & 0 deletions src/lfortran/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif // NOMINMAX
#include <windows.h>
#endif

#include <fstream>

#include <bin/tpl/whereami/whereami.h>
Expand Down Expand Up @@ -57,5 +64,75 @@ std::string get_runtime_library_header_dir()
return get_runtime_library_dir() + "/impure";
}

bool read_file(const std::string &filename, std::string &text)
{
std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary
| std::ios::ate);

std::ifstream::pos_type filesize = ifs.tellg();
if (filesize < 0) return false;

ifs.seekg(0, std::ios::beg);

std::vector<char> bytes(filesize);
ifs.read(&bytes[0], filesize);

text = std::string(&bytes[0], filesize);
return true;
}

bool present(Vec<char*> &v, const char* name) {
for (auto &a : v) {
if (std::string(a) == std::string(name)) {
return true;
}
}
return false;
}

Platform get_platform()
{
#ifdef _WIN32
return Platform::Windows;
#else
# ifdef __APPLE__
# ifdef __aarch64__
return Platform::macOS_ARM;
# else
return Platform::macOS_Intel;
# endif
# else
# ifdef __FreeBSD__
return Platform::FreeBSD;
# else
return Platform::Linux;
# endif
# endif
#endif
}

// Platform-specific initialization
// On Windows, enable colors in terminal. On other systems, does nothing.
// Return value: 0 on success, negative number on failure.
int initialize()
{
#ifdef _WIN32
HANDLE h_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
if (h_stdout == INVALID_HANDLE_VALUE)
return -1;

DWORD mode;
if (! GetConsoleMode(h_stdout, &mode))
return -2;

mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (! SetConsoleMode(h_stdout, mode))
return -3;

return 0;
#else
return 0;
#endif
}

}

0 comments on commit bc7ef88

Please sign in to comment.