--- a/setup.py	2015-09-22 07:26:00.000000000 +0200
+++ b/setup.py	2016-02-27 18:01:10.582251583 +0100
@@ -6,12 +6,28 @@
 import os
 
 # Modifiy this if BLAS and LAPACK libraries are not in /usr/lib.
-BLAS_LIB_DIR = '/usr/lib'
+import pkgconfig
+
+blas_pc = pkgconfig.parse('blas')
+lapack_pc = pkgconfig.parse('lapack')
+gsl_pc = pkgconfig.parse('gsl')
+
+SAGE_LIB = os.path.join(os.environ['SAGE_LOCAL'], 'lib')
+SAGE_INCLUDE = os.path.join(os.environ['SAGE_LOCAL'], 'include')
 
 # Default names of BLAS and LAPACK libraries
-BLAS_LIB = ['blas']
-LAPACK_LIB = ['lapack']
+LAPACK_LIB = list(lapack_pc['libraries'])
+
 BLAS_EXTRA_LINK_ARGS = []
+if os.environ['UNAME'] == 'CYGWIN':
+    BLAS_LIB =['blas', 'gfortran']
+    BLAS_LIB_DIR = '/usr/lib'
+else:
+    BLAS_LIB = list(blas_pc['libraries'])
+    try:
+        BLAS_LIB_DIR = next(iter(blas_pc['library_dirs']))
+    except StopIteration:
+        BLAS_LIB_DIR = SAGE_LIB
 
 # Set environment variable BLAS_NOUNDERSCORES=1 if your BLAS/LAPACK do
 # not use trailing underscores
@@ -19,13 +35,19 @@
 
 # Set to 1 if you are using the random number generators in the GNU
 # Scientific Library.
-BUILD_GSL = 0
+BUILD_GSL = 1
 
 # Directory containing libgsl (used only when BUILD_GSL = 1).
-GSL_LIB_DIR = '/usr/lib'
+try:
+    GSL_LIB_DIR = next(iter(gsl_pc['library_dirs']))
+except StopIteration:
+    GSL_LIB_DIR = SAGE_LIB
 
 # Directory containing the GSL header files (used only when BUILD_GSL = 1).
-GSL_INC_DIR = '/usr/include/gsl'
+try:
+    GSL_INC_DIR = next(iter(gsl_pc['include_dirs']))
+except StopIteration:
+    GSL_INC_DIR = SAGE_INCLUDE
 
 # Set to 1 if you are installing the fftw module.
 BUILD_FFTW = 0 
@@ -37,13 +59,13 @@
 FFTW_INC_DIR = '/usr/include'
 
 # Set to 1 if you are installing the glpk module.
-BUILD_GLPK = 0 
+BUILD_GLPK = 1
 
 # Directory containing libglpk (used only when BUILD_GLPK = 1).
-GLPK_LIB_DIR = '/usr/lib'
+GLPK_LIB_DIR = SAGE_LIB
 
 # Directory containing glpk.h (used only when BUILD_GLPK = 1).
-GLPK_INC_DIR = '/usr/include'
+GLPK_INC_DIR = SAGE_INCLUDE
 
 # Set to 1 if you are installing the DSDP module.
 BUILD_DSDP = 0
@@ -114,7 +136,7 @@
     extmods += [fftw];
 
 if BUILD_GLPK:
-    glpk = Extension('glpk', libraries = ['glpk'],
+    glpk = Extension('glpk', libraries = ['glpk', 'gmp', 'z'],
         include_dirs = [ GLPK_INC_DIR ],
         library_dirs = [ GLPK_LIB_DIR ],
         sources = ['src/C/glpk.c'] )
@@ -213,7 +235,7 @@
     amd = Extension('amd', 
         include_dirs = [ 'src/C/SuiteSparse/AMD/Include', 
             'src/C/SuiteSparse/SuiteSparse_config' ],
-        define_macros = MACROS,
+        define_macros = MACROS  + [('NTIMER', '1')],  # this will be in upstream at some point
         sources = [ 'src/C/amd.c', 'src/C/SuiteSparse/SuiteSparse_config/SuiteSparse_config.c'] +
         glob('src/C/SuiteSparse/AMD/Source/*.c') )
 
