How to have multiple build flavors with stack? - haskell

When developing, I want to build everything with -O0 to save time.
But when building the same package on a CI system, I want to use -O.
One option I see would be to have two different stack.yaml files differing in their ghc-options, but then I'd have to make all changes to stack.yaml in two places.
Having an alias for stack --ghc-options=-O0 doesn't work, because I want -O0 to apply only to local packages, not the snapshot, and there's no way to specify that on the command line, from what I know.
Are there any better options?

Actually, stack build --ghc-options=-O0 will apply -O0 to only the local packages, not to the snapshot packages. For more information, see:
https://github.com/commercialhaskell/stack/blob/master/GUIDE.md#ghc-options

Related

Bash command: export BLAS_LIBS="-L$LAPACKHOME/lib -lblas"

Can any body explain to me what does the whole sentence mean?
I know this is to set Macro BLAS_LIBS as another string.
But I'm not sure what's the "-lblas" mean and I don't know how to use it.
Similar as the following code. "-llapack"
export LAPACK_LIBS="-L$LAPACKHOME/lib -llapack"
How can the program find out the BLAS and LAPACK libraries just by "-lblas" and "-llapack" ?
Thanks for advance.
I'm not sure why you say "just by -llapack" because that's not what is happening here. Specifically, the -L option just before it specifies a directory path to add to the library resolution path. This works roughly like PATH in the shell.
For example, with the command line fragment gcc -Lfoodir -Lbardir -lfoo -lbar, you basically instruct the linker to search the directories foodir and bardir for the library files libfoo.a and libbar.a.
The -l option is described in GCC: Options for Linking and -L and friends in the following section GCC: Options for Directory Search.
This build arrangement -- configure the build to show where the required files are before compiling -- is common for libraries, where if a user has already downloaded and compiled a required library for some other project, they don't need to rebuild it; they can just point the compiler to wherever they already have the stuff needed for this project.
Building your own libraries is becoming increasingly unnecessary anyway, as prepackaged binaries of most common libraries are available for most systems these days. But of course, if you are on an unusual platform, or have specialized needs which dictate recompilation with different options than any available prebuilt binary, you will still need to understand how to do this.

Nominate library directory at compilation

I was able to generate an executable using
gcc myexec -o obj1.obj -o obj2.obj ...and link xxx.dylib
I deploy myexec to a different machine. myexec, ofcourse, would need xxx.dylib to run.
For that, I create myInstallFolder with xxx.dylib and myexec
On deployment, I was able to make myexec pick xxx.dylib by setting DYLD_LIBRARY_PATH point to myInstallFolder.
However, I want to avoid using DYLD_LIBRARY_PATH.
What change should be done at compilation/linking to make myexec pick up from myInstallFolder?
~Ryder
You can instruct the linker to write a path into the binary that the loader will use to load .so files:
-Wl,-R<path to myInstallFolder>
For example
-Wl,-R/usr/local/mybin/lib

how to use my own dynamic library in linux (Makefile)

I have a c++ project (g++/raw Makefile) designed for linux, I used to statically link everything which worked fine for ages. Now I want to build binaries both statically and dynamically linked. The following command is used in my Makefile to build the dynamic library (say libtest):
$(CXX) -shared -Wl,-soname,libtest.so.1 -o libtest.so.1.0.0 $(LIBTEST_OBJS)
The output is libtest.so.1.0.0 which has the so name libtest.so.1
I found at least a symbolic link libtest.so --> libtest.so.1.0.0 is required to link my client program that actually use the above generated libtest.so.1.0.0 library.
Here my question is if I want to build my software, what is the standard way of managing the above symbolic link? Clearly I don't want this extra stuff in my source directory, but it is required to build my client binary, shall I create it as a temp link for building the client then just remove it when done? or shall I create a directory to host the generate .so library and its links and leave everything there until I do "make install" to install them into other specified directories? Will be cool to now what is the standard way of doing this.
Or maybe the way how I generate libraries is incorrect? shall I just generate libtest.so (as actual library, not a link) to link my executable, then rename the library and create those links when doing ``make install''?
any input will be appreciated. :)
Certainly don't generate libtest.so as an actual link. Typically installing the shared library development files installs the .h files and creates a symbolic link libtest.so as part of some install script you have to write.
If you're not installing the development files, but only using the library in your build process of your binary, you just create the symbolik link from your makefile.
There's not that much of a standard here, some prefer to build artifacts to a separate build directory,
some don't care if it's built in the source directory. I'd build to a separate directory though, and keep the source directory clean of any .o/.so/executable files.
You might find useful information here
My suggestion is to use libtool which handles situations like this.

Store GNU make generated files elsewhere

How can I store GNU make & configure files elsewhere? I have a project I am working on that I get compiled using:
./configure --prefix=/usr && make && su -c 'make install'
The thing is I don't want to pollute the current folder, which is a svn/git/hg/whatever sandbox with files generated by that command. I want to put those files in a separate location. I don't know if it's similar, but when I compile the linux kernel from source, I can specify where to put the ouput by passing the 'O' option to 'make', something like this:
make O=/home/user/linux-output
The Makefile must support this feature.
I think the autoconf generated makefiles all support the following use:
mkdir ../build
cd ../build
../configure --prefix=/usr
make
make install
(It's certainly recommended for gcc builds).
As Kristof already pointed out, GNU autotools inherently support out out-of-tree builds at the configure level.
So you can have the Makefile and built binaries out of the source tree trivially.
To get all the auto-generated artefacts out of the source tree requires much more work however.
We have a script that copies changes from a source tree into a working_copy, carefully preserving the configure script etc in the working_copy, which allows the original source tree to be pristine. However it's very inefficient, so I wouldn't recommend it.
I would recommend a normal out-of-tree build, and then explicitly excluding the remaining auto-generated files in the source tree.

How to stop GHC from generating intermediate files?

When compiling a haskell source file via ghc --make foo.hs GHC always leaves behind a variety of intermediate files other than foo.exe. These are foo.hi and foo.o.
I often end up having to delete the .hi and .o files to avoid cluttering up the folders.
Is there a command line option for GHC not to leave behind its intermediate files? (When asked on #haskell, the best answer I got was ghc --make foo.hs && rm foo.hi foo.o.
I've gone through the GHC docs a bit, and there doesn't seem to be a built-in way to remove the temporary files automatically -- after all, GHC needs those intermediate files to build the final executable, and their presence speeds up overall compilation when GHC knows it doesn't have to recompile a module.
However, you might find that setting the -outputdir option will help you out; that will place all of your object files (.o), interface files (.hi), and FFI stub files in the specified directory. It's still "clutter," but at least it's not in your working directory anymore.
GHC now has the options no-keep-hi-files and no-keep-o-files. See here for more information.
My usual workflow is to use cabal rather than ghc directly. This sets the outputdir option into an appropriate build folder and can do things like build haddock documentation for you. All you need is to define the .cabal file for your project and then say cabal install or cabal build instead of run ghc directly. Since you need to follow this process in the end if you ever want to share your work on hackage, it is a good practice to get into and it helps manage package dependencies as well.
You can set the -hidir to /dev/null, I think, sending them there. Also, the -fno-code option in general turns off a lot of output. You might just want to use Cabal.
Turns out that using -hidir/-odir/-outputdir is no good; /dev/null is a file, and not a directory. See http://www.haskell.org/pipermail/xmonad/2010-May/010182.html
2 cents to improve the workflow a bit:
We can put the following alias into the .bashrc (or similar) config
file:
alias hsc='_hsc(){ ghc -no-keep-hi-files -no-keep-o-files "$#";}; _hsc'
And then just call
$ hsc compose.hs
[1 of 1] Compiling Main ( compose.hs, compose.o )
Linking compose ...
$ ls
compose compose.hs

Resources