Flymake help

From OpenCog

Jump to: navigation, search

Contents

What is flymake?

Flymake is an emacs minor mode to compile check the code as you type it. It is nice to spot errors (or warnings) on the fly, check that video for an example.

How to install flymake for OpenCog?

Flymake does not support CMake natively, so I've written a script that generates automatically the Makefiles based on the CMakeLists.txts.

(If you use VIM, you can get a version of flymake from http://github.com/kana/vim-flymake )

Now copy these 2 scripts on your machine:

#!/bin/bash
#
# generateMakefiles.sh by Nil Geisweiller
#
# That script generates Makefile.flymake (or rather Makefile for now)
# all over the CMake project.
# It takes 1 argument, the name of the building dir
if [ ! -n "${1}" ]; then
    echo "Wrong number of argument"
    echo "Usage: ${0} BUILD_DIR_NAME"
    exit 1
else
    BUILD_NAME="${1}"
fi
 
#MAKEFILE_NAME="Makefile.flymake"
MAKEFILE_NAME="Makefile"
 
# for each CMakeLists.txt found from the current directory
# create the associated Makefile.flymake (or rather Makefile for now).
while read -r -d $'\0' FILEPATH
do
    RELATIVE_MAKEFILE_DIR=${FILEPATH%/*}
    LAST_DIR=${RELATIVE_MAKEFILE_DIR##*/}
    FLAGS_DIR="${PWD}/${BUILD_NAME}/${RELATIVE_MAKEFILE_DIR}/CMakeFiles/${LAST_DIR}.dir/"
    # write the content of the Makefile
    echo "# Automatically generated by ${0}, you may remove it using removeMakefiles.sh as you please" > "${RELATIVE_MAKEFILE_DIR}/${MAKEFILE_NAME}"
    echo "include ${FLAGS_DIR}flags.make" >> "${RELATIVE_MAKEFILE_DIR}/${MAKEFILE_NAME}"
    echo "check-syntax:" >> "${RELATIVE_MAKEFILE_DIR}/${MAKEFILE_NAME}"
    echo -e "\tc++ -o /dev/null \${CXX_FLAGS} \${CXX_DEFINES} -S \${CHK_SOURCES}" >> "${RELATIVE_MAKEFILE_DIR}/${MAKEFILE_NAME}"
    echo ".PHONY: check-syntax" >> "${RELATIVE_MAKEFILE_DIR}/${MAKEFILE_NAME}"    
done < <( find . -name "CMakeLists.txt" -print0 )
 
exit 0
#!/bin/bash
#
# removeMakefiles.sh by Nil Geisweiller
#
# That script remove recursively all Makefile.flymake
# (or rather Makefile for now) from the CMake project.
 
#MAKEFILE_NAME="Makefile.flymake"
MAKEFILE_NAME="Makefile"
 
# For each CMakeLists.txt found from the current directory and descendants
# it removes the associated Makefile.flymake (or rather Makefile for now).
while read -r -d $'\0' FILEPATH
do
    RELATIVE_MAKEFILE_DIR=${FILEPATH%/*}
    rm "${RELATIVE_MAKEFILE_DIR}/${MAKEFILE_NAME}"
done < <( find . -name "CMakeLists.txt" -print0 )

How to use flymake?

  1. Before you can generate the Makefiles you must first compile your CMake project, or rather you must at least run cmake in the building directory because generateMakefiles.sh relies on information produced by cmake. So of course whenever you modify a CMakeLists.txt you will need to recompile the project (or at least run cmake) and regenerate the Makefiles.
  2. Run generateMakefiles.sh from the project root directory, note it is important to run it from the project root directory as the script relies on $PWD. You need to specify the name of the building directory in argument, for example if your building directory is bin, type:
     ./generateMakefiles.sh bin
  3. Open your code with emacs, load flymake (M-x flymake-mode), edit your code and spot errors on the fly :-). Note that depending on the size of the file and the speed of your machine it can take a few seconds to spot the error (watch out the star appended to "flymake" in the buffer name bar to know whether flymake is currently checking errors).
  4. Once you're done you still have Makefiles all over the project. If you want to remove them use the script removeMakefiles.sh. It looks for all CMakeFiles.txt and for everyone removes the Makefile beside it.

Future improvements

  • I wanted to use Makefile.flymake instead of Makefile not to overwrite possibly existing Makefile, but making flymake using them turned out to be harder than I thought. I'll fix this eventually, by then just be careful where you run that script to not replace or erase some Makefile unwillingly. Note that in principle in any sane configuration of a CMake project a Makefile should not be beside a CMakeLists.txt, so you should be safe most of the time.
  • .cxxtest files are not supported yet.