OpenFOAM with Cygwin ==================== Petr Vita Niklas Nordin Ron W Cresswell This document describes how to get core of OpenFOAM compiled and runing under Cygwin. Thanks to all who helped. Table of Contents ----------------- 1 Mission 2 How it works? 2.1 Case-insensitive filenames 2.2 Difference in compilation of so-/dll-files 3 Prerequisites 4 Preinstall 5 Installation 6 Compilation 6.1 Compilation with Windows 2000 Professional 7 Running applications 8 Works it without Cygwin? 9 References 1 Mission ---------- ...and the Lord spoke to stones: "Do you want to get OpenFOAM to work under Cygwin?" Stones answered: "No our Lord, we are not hard enough!" My task was to get a core OpenFOAM library and applications to work under Cygwin. It is not my interest and intent to get pre-processing/post-processing front-ends to work and to show the way. The most important requirement was not to break actual compilation system, just to enhance it so it can co-exist with Unix/Linux distribution. If there are errors, they are there to be found and removed. 2 How it works? ---------------- Way it is not so trivial to get OpenFOAM under the Cygwin? The first thing that could be seen as trivial are ambiguos filenames that make problems in case-insensitive operating systems in general. The second problem is a difference between the compilation of shared object libraries under Windows and Linux/Unix. 2.1 Case-insensitive filenames ------------------------------ One could get into headaches just trying to unpack the OpenFOAM source distribution. In order to get rid of it one can use Cygwin's managed mounts to solve placemenet of OpenFOAM source files [1]. The whole compilation is done in managed mount producing executables and libraries on non-managed mount that is case-insensitive. 2.2 Difference in compilation of so-/dll-files ---------------------------------------------- Unix and Windows use completely different paradigms for dynamically loaded code. This difference has direct influence on how the compilation work. Unix so-file contains code used by the program and the names of the functions and data that it expects to find in the program. When a so-file is loaded, all references to those functions and data in the file's code are changed to point to actual locations in the program where the functions and data are placed in memory. In Windows a dll-file hos no such references. A look-up table is used to access functions and data. dll-file's code does not have to be fixed up at runtime to refer to programs memory, instead to program's code already uses dll-file's lookup table and this lookup table is modified to point to the functions and data. In order to create a dll-file under Windows compiler has to construct dll-file's lookup table during compilation. To do it it needs information about the program that will reassure him that there are data and functions it needs. This information is hidden in so called import library. When application or dll-files is linked linker will generate this import library automatically, see [2] for more info. In principle we need to fix a compilation procedure to provide all information linker needs to generate dll-file's lookup table. Normally we just need to add an import library to compile a code unit. Thing go complicated in a case of cyclic dependencies. If one dll-file needs another one to run and vice versa we need to provide linker even with an object code of files that will be linked into a yet non-existent dll-file. This is not necessary under Linux. To fix it OpenFOAM's Makefile have to be augmented to reflect this new requirements as well as OpenFOAM's mechanism for dependecy analysis. All this things are hidden in a patch provided and should have no influence on Linux version. 3 Prerequisites --------------- To get whole compilation to work you will need to download some files from internet. You will have to get source of GNU GCC compiler (a provided script expect version 4.0.2), OpenFOAM sources (version 1.2 is expected), a provided patch, a script preInstall.sh and gccCompile.sh to succesfully finish pre-install phase and do not forget to install Cygwin 1.5.19-4 and up. You should expect problems everywhere. Checklist: - Cygwin 1.5.19-4 or higher [ http://www.cygwin.com ] installed in C:\Cygwin - GNU GCC 4.0.2 source files [ http://gcc.gnu.org ] in ~/ gcc-4.0.2.tar.gz gcc-core-4.0.2.tar.gz gcc-g++-4.0.2.tar.gz - OpenFOAM 1.2 source file [ http://www.opencfd.co.uk/openfoam ] in ~/ OpenFOAM-1.2.General.gtgz - patch file in ~/ OpenFOAM-1.2.Cygwin.patch.gz - compilation and pre-install scripts in ~/ gccCompile.sh preInstall.sh - coffee, aspirin, some paper for notices and some book to read If all is on the place, pray to Cygwin and Windows Gods. You are ready to start now! 4 Preinstall ------------ Be sure you have all prerequisities and no directories /managed-dir, ~/OpenFOAM, /opt/gcc-4.0.2, ~/gcc-4.0.2 and ~/gcc-objdir. All you need is to start pre-install script in your ~/ directory typing: ./preInstall.sh Launching of this script will unpack, configure, compile and install GNU GCC compiler in /opt directory, creates managed mount for OpenFOAM sources, extracts and patches OpenFOAM distribution. It takes some time so drink coffee and read book, so far there should be no problem. You can drop ~/gcc-* directories if all went well you won't need them anymore. If something goes wrong, inspect preInstall.sh and gccCompile.sh scripts and correct what needs to be corrected. 5 Installation -------------- All variables and environment settings are described in OpenFOAM's README so read to know what to do. The Cygwin patch file will do quite a lot itself but you have to at least select right compilation target (Opt, Debug or Prof). Do not be surprised that destination directory of libraries and executables points out of the managed mount, it is intended. Start new shell or execute bashrc script to source and update variables. If all done well you should have directories ~/OpenFOAM/bin, ~/OpenFOAM/lib with their compilation targets inside. You should not have a time for book but drinking coffee helps so do it. 6 Compilation ------------- Things will start to be tricky now as you have to compile whole OpenFOAM and its application. I did compilation under Windows 2000 Professional only so do not ask me about other version. I do not like and do not know Windows operating system so spare me of question about it as well. 6.1 Compilation with Windows 2000 Professional ---------------------------------------------- Step-by-step instructions follow: 1 Close all application you really do not need. I mean all! 2 Type-in cd ~/OpenFOAM/OpenFOAM-1.2 to place yourself in the best shooting position. 3 Get task manager window up so you can observe what happens. 4 Type-in ./Allwmake and observe compilation output and task manager. You will get to point where OpenFOAM will try to link all its headers and source files into directory ~/OpenFOAM/OpenFOAM-1.2/src/OpenFOAM/lnInclude. 4a Compilation got stucked! Nothing happens for a very long time. The task manager show process ln takes near 99% of CPU time, all in user space only. You are damned. Follow instructions in point 5. 4b Compilation continues producing foamFlex++, libPstream.dll, libOpenFOAM.dll etc. There is any known problem now. Read book, drink coffee, you have around 3-4 hours time now. Cheer! Continue with point 7. 5 Make a note on the paper you are here. 5a There is just one note on the paper. Immediately swallow the aspirin, get new coffee a be ready for hard fight. You should know, that the aspirin will need some time to work. Take your time, you do not need to collapse. Follow with point 6. 5b If there is more then one notice, do not swallow the aspirin (an aspirin overdosis is dangerous) and start to panic. You are lost! I have no idea what you should do as next step. Google, make call to telephone guru, ask public... 6 Break compilation using Ctrl+C. Kill manually running process ln. If everything goes well, Windows are going to run quite mad now. Task manager shows that process Explorer.exe runs amoc taking now 99% of CPU time. No hesitation, he desires it as well. Kill process Explorer.exe. You loose taks bar, desktop and whole that pretty stuff but do not worry, Windows are back stabile again. A positive point is that compilation runs more quickly now. You have to kill all process Run32.exe as well. Manually check if there is a file FlexLexer.h in ~/OpenFOAM/OpenFOAM-1.2/src/OpenFOAM/lnInclude directory. If no, copy it there from ~/OpenFOAM/OpenFOAM-1.2/applications/utilities/miscallenous/foamFlex directory. You should get through linking of headers and sources now. Follow instructions in point 2 trying it once again. 7 Restart your machine if your notice paper contains a note you have made in point 5. You should have, or have not, compiled OpenFOAM on you machine. In the positive case you have around 90 library files and around 160 applications ready to use. 7 Running applications ---------------------- All applications should run now. I tested just one I needed if there are some specific problems, report them to Wiki. You can try to solve them at the best case and share your solution with other. Normally are executables and libraries placed in ~/OpenFOAM/bin and ~/OpenFOAM/lib folders and are set in your PATH. You can move them in another directory as you wish but managed mount. Cygwin is using Windows mechanism to load dll-files so placing binaries with case sensitive name on managed mount won't work. 8 Works it without Cygwin? -------------------------- No. You always need Cygwin emulation layer and it is not possible to use MinGW with managed mounts now in compilation phase. But! You can use OpenFOAM dll-files without full Cygwin installation if you have just cygwin1.dll with them. Unfortunately you will have to hack the code as well as there is some hardcoded stuff that depends on environment settings. But it should not be problematic a lot. 9 References ------------ [1] Cygwin FAQ http://cygwin.com/fag.html [2] Windows vs. Unix: Linking dynamic load modules http://cphoenix.best.vwh.net/winvunix.html [3] OpenFOAM Forum http://openfoam.cfd-online.com/cgi-bin/forum/discus.cgi