Repository: spack/spack

Issue:
Refactor code shared between CMakePackage and general builds using CMake.
@alalazo I noticed that `get_std_cmake_args()` in `build_environment.py` seems to repeat code from `_std_args()` in `build_systems/cmake.py`.  Can one of them be deleted?


Discussion:
Any `Package` subclass can still use cmake.  Maybe there's a decent way to refactor but I don't think we should get rid of these variables for general builds.
Oh yes, of course.  Let's just say this should be refactored.

Resolving patch:
diff --git a/lib/spack/spack/build_environment.py b/lib/spack/spack/build_environment.py
--- a/lib/spack/spack/build_environment.py
+++ b/lib/spack/spack/build_environment.py
@@ -432,19 +432,15 @@ def get_rpaths(pkg):
     return rpaths
 
 
-def get_std_cmake_args(cmake_pkg):
-    # standard CMake arguments
-    ret = ['-DCMAKE_INSTALL_PREFIX=%s' % cmake_pkg.prefix,
-           '-DCMAKE_BUILD_TYPE=RelWithDebInfo',
-           '-DCMAKE_VERBOSE_MAKEFILE=ON']
-    if platform.mac_ver()[0]:
-        ret.append('-DCMAKE_FIND_FRAMEWORK=LAST')
-
-    # Set up CMake rpath
-    ret.append('-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=FALSE')
-    ret.append('-DCMAKE_INSTALL_RPATH=%s' % ":".join(get_rpaths(cmake_pkg)))
-
-    return ret
+def get_std_cmake_args(pkg):
+    """Returns the list of standard arguments that would be used if this
+    package was a CMakePackage instance.
+
+    :param pkg: pkg under consideration
+
+    :return: list of arguments for cmake
+    """
+    return spack.CMakePackage._std_args(pkg)
 
 
 def parent_class_modules(cls):
diff --git a/var/spack/repos/builtin.mock/packages/cmake-client/package.py b/var/spack/repos/builtin.mock/packages/cmake-client/package.py
--- a/var/spack/repos/builtin.mock/packages/cmake-client/package.py
+++ b/var/spack/repos/builtin.mock/packages/cmake-client/package.py
@@ -32,15 +32,13 @@ def check(condition, msg):
         raise InstallError(msg)
 
 
-class CmakeClient(Package):
+class CmakeClient(CMakePackage):
     """A dumy package that uses cmake."""
     homepage  = 'https://www.example.com'
     url       = 'https://www.example.com/cmake-client-1.0.tar.gz'
 
     version('1.0', '4cb3ff35b2472aae70f542116d616e63')
 
-    depends_on('cmake', type='build')
-
     def setup_environment(self, spack_env, run_env):
         spack_cc    # Ensure spack module-scope variable is avaiabl
         check(from_cmake == "from_cmake",
@@ -68,6 +66,11 @@ def setup_dependent_package(self, module, dspec):
               "link arg on dependency spec not readable from "
               "setup_dependent_package.")
 
+    def cmake(self, spec, prefix):
+        pass
+
+    build = cmake
+
     def install(self, spec, prefix):
         # check that cmake is in the global scope.
         global cmake


Test patch:
diff --git a/lib/spack/spack/test/build_systems.py b/lib/spack/spack/test/build_systems.py
new file mode 100644
--- /dev/null
+++ b/lib/spack/spack/test/build_systems.py
@@ -0,0 +1,42 @@
+##############################################################################
+# Copyright (c) 2013-2016, Lawrence Livermore National Security, LLC.
+# Produced at the Lawrence Livermore National Laboratory.
+#
+# This file is part of Spack.
+# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
+# LLNL-CODE-647188
+#
+# For details, see https://github.com/llnl/spack
+# Please also see the LICENSE file for our notice and the LGPL.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License (as
+# published by the Free Software Foundation) version 2.1, February 1999.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
+# conditions of the GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+##############################################################################
+
+import spack
+from spack.build_environment import get_std_cmake_args
+from spack.spec import Spec
+
+
+def test_cmake_std_args(config, builtin_mock):
+    # Call the function on a CMakePackage instance
+    s = Spec('cmake-client')
+    s.concretize()
+    pkg = spack.repo.get(s)
+    assert pkg.std_cmake_args == get_std_cmake_args(pkg)
+
+    # Call it on another kind of package
+    s = Spec('mpich')
+    s.concretize()
+    pkg = spack.repo.get(s)
+    assert get_std_cmake_args(pkg)