1# This macro set originally copied from Open MPI:
2# Copyright © 2004-2007 The Trustees of Indiana University and Indiana
3#                         University Research and Technology
4#                         Corporation.  All rights reserved.
5# Copyright © 2004-2005 The University of Tennessee and The University
6#                         of Tennessee Research Foundation.  All rights
7#                         reserved.
8# Copyright © 2004-2007 High Performance Computing Center Stuttgart,
9#                         University of Stuttgart.  All rights reserved.
10# Copyright © 2004-2005 The Regents of the University of California.
11#                         All rights reserved.
12# and renamed for hwloc:
13# Copyright © 2009 Inria.  All rights reserved.
14# Copyright © 2009 Université Bordeaux
15# Copyright © 2010 Cisco Systems, Inc.  All rights reserved.
16# See COPYING in top-level directory.
17#
18# Redistribution and use in source and binary forms, with or without
19# modification, are permitted provided that the following conditions are
20# met:
21#
22# - Redistributions of source code must retain the above copyright
23#   notice, this list of conditions and the following disclaimer.
24#
25# - Redistributions in binary form must reproduce the above copyright
26#   notice, this list of conditions and the following disclaimer listed
27#   in this license in the documentation and/or other materials
28#   provided with the distribution.
29#
30# - Neither the name of the copyright holders nor the names of its
31#   contributors may be used to endorse or promote products derived from
32#   this software without specific prior written permission.
33#
34# The copyright holders provide no reassurances that the source code
35# provided does not infringe any patent, copyright, or any other
36# intellectual property rights of third parties.  The copyright holders
37# disclaim any liability to any recipient for claims brought against
38# recipient by any third party for infringement of that parties
39# intellectual property rights.
40#
41# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
42# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
43# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
44# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
45# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
48# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
49# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
51# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52#
53
54#
55# Search the generated warnings for
56# keywords regarding skipping or ignoring certain attributes
57#   Intel: ignore
58#   Sun C++: skip
59#
60AC_DEFUN([_HWLOC_ATTRIBUTE_FAIL_SEARCH],[
61    # Be safe for systems that have ancient Autoconf's (e.g., RHEL5)
62    m4_ifdef([AC_PROG_GREP],
63             [AC_REQUIRE([AC_PROG_GREP])],
64             [GREP=grep])
65
66    if test -s conftest.err ; then
67        for i in ignore skip ; do
68            $GREP -iq $i conftest.err
69            if test "$?" = "0" ; then
70                hwloc_cv___attribute__[$1]=0
71                break;
72            fi
73        done
74    fi
75])
76
77#
78# HWLOC: Remove C++ compiler check.  It can result in a circular
79# dependency in embedded situations.
80#
81# Check for one specific attribute by compiling with C
82# and possibly using a cross-check.
83#
84# If the cross-check is defined, a static function "usage" should be
85# defined, which is to be called from main (to circumvent warnings
86# regarding unused function in main file)
87#       static int usage (int * argument);
88#
89# The last argument is for specific CFLAGS, that need to be set
90# for the compiler to generate a warning on the cross-check.
91# This may need adaption for future compilers / CFLAG-settings.
92#
93AC_DEFUN([_HWLOC_CHECK_SPECIFIC_ATTRIBUTE], [
94    AC_MSG_CHECKING([for __attribute__([$1])])
95    AC_CACHE_VAL(hwloc_cv___attribute__[$1], [
96        #
97        # Try to compile using the C compiler
98        #
99        AC_TRY_COMPILE([$2],[],
100                       [
101                        #
102                        # In case we did succeed: Fine, but was this due to the
103                        # attribute being ignored/skipped? Grep for IgNoRe/skip in conftest.err
104                        # and if found, reset the hwloc_cv__attribute__var=0
105                        #
106                        hwloc_cv___attribute__[$1]=1
107                        _HWLOC_ATTRIBUTE_FAIL_SEARCH([$1])
108                       ],
109                       [hwloc_cv___attribute__[$1]=0])
110
111        #
112        # If the attribute is supported by both compilers,
113        # try to recompile a *cross-check*, IFF defined.
114        #
115        if test '(' "$hwloc_cv___attribute__[$1]" = "1" -a "[$3]" != "" ')' ; then
116            ac_c_werror_flag_safe=$ac_c_werror_flag
117            ac_c_werror_flag="yes"
118            CFLAGS_safe=$CFLAGS
119            CFLAGS="$CFLAGS [$4]"
120
121            AC_TRY_COMPILE([$3],
122                [
123                 int i=4711;
124                 i=usage(&i);
125                ],
126                [hwloc_cv___attribute__[$1]=0],
127                [
128                 #
129                 # In case we did NOT succeed: Fine, but was this due to the
130                 # attribute being ignored? Grep for IgNoRe in conftest.err
131                 # and if found, reset the hwloc_cv__attribute__var=0
132                 #
133                 hwloc_cv___attribute__[$1]=1
134                 _HWLOC_ATTRIBUTE_FAIL_SEARCH([$1])
135                ])
136
137            ac_c_werror_flag=$ac_c_werror_flag_safe
138            CFLAGS=$CFLAGS_safe
139        fi
140    ])
141
142    if test "$hwloc_cv___attribute__[$1]" = "1" ; then
143        AC_MSG_RESULT([yes])
144    else
145        AC_MSG_RESULT([no])
146    fi
147])
148
149
150#
151# Test the availability of __attribute__ and with the help
152# of _HWLOC_CHECK_SPECIFIC_ATTRIBUTE for the support of
153# particular attributes. Compilers, that do not support an
154# attribute most often fail with a warning (when the warning
155# level is set).
156# The compilers output is parsed in _HWLOC_ATTRIBUTE_FAIL_SEARCH
157#
158# To add a new attributes __NAME__ add the
159#   hwloc_cv___attribute__NAME
160# add a new check with _HWLOC_CHECK_SPECIFIC_ATTRIBUTE (possibly with a cross-check)
161#   _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([name], [int foo (int arg) __attribute__ ((__name__));], [], [])
162# and define the corresponding
163#   AC_DEFINE_UNQUOTED(_HWLOC_HAVE_ATTRIBUTE_NAME, [$hwloc_cv___attribute__NAME],
164#                      [Whether your compiler has __attribute__ NAME or not])
165# and decide on a correct macro (in opal/include/opal_config_bottom.h):
166#  #  define __opal_attribute_NAME(x)  __attribute__(__NAME__)
167#
168# Please use the "__"-notation of the attribute in order not to
169# clash with predefined names or macros (e.g. const, which some compilers
170# do not like..)
171#
172
173
174AC_DEFUN([_HWLOC_CHECK_ATTRIBUTES], [
175  AC_MSG_CHECKING(for __attribute__)
176
177  AC_CACHE_VAL(hwloc_cv___attribute__, [
178    AC_TRY_COMPILE(
179      [#include <stdlib.h>
180       /* Check for the longest available __attribute__ (since gcc-2.3) */
181       struct foo {
182           char a;
183           int x[2] __attribute__ ((__packed__));
184        };
185      ],
186      [],
187      [hwloc_cv___attribute__=1],
188      [hwloc_cv___attribute__=0],
189    )
190
191    if test "$hwloc_cv___attribute__" = "1" ; then
192        AC_TRY_COMPILE(
193          [#include <stdlib.h>
194           /* Check for the longest available __attribute__ (since gcc-2.3) */
195           struct foo {
196               char a;
197               int x[2] __attribute__ ((__packed__));
198            };
199          ],
200          [],
201          [hwloc_cv___attribute__=1],
202          [hwloc_cv___attribute__=0],
203        )
204    fi
205    ])
206  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE, [$hwloc_cv___attribute__],
207                     [Whether your compiler has __attribute__ or not])
208
209#
210# Now that we know the compiler support __attribute__ let's check which kind of
211# attributed are supported.
212#
213  if test "$hwloc_cv___attribute__" = "0" ; then
214    AC_MSG_RESULT([no])
215    hwloc_cv___attribute__aligned=0
216    hwloc_cv___attribute__always_inline=0
217    hwloc_cv___attribute__cold=0
218    hwloc_cv___attribute__const=0
219    hwloc_cv___attribute__deprecated=0
220    hwloc_cv___attribute__format=0
221    hwloc_cv___attribute__hot=0
222    hwloc_cv___attribute__malloc=0
223    hwloc_cv___attribute__may_alias=0
224    hwloc_cv___attribute__no_instrument_function=0
225    hwloc_cv___attribute__nonnull=0
226    hwloc_cv___attribute__noreturn=0
227    hwloc_cv___attribute__packed=0
228    hwloc_cv___attribute__pure=0
229    hwloc_cv___attribute__sentinel=0
230    hwloc_cv___attribute__unused=0
231    hwloc_cv___attribute__warn_unused_result=0
232    hwloc_cv___attribute__weak_alias=0
233  else
234    AC_MSG_RESULT([yes])
235
236    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([aligned],
237        [struct foo { char text[4]; }  __attribute__ ((__aligned__(8)));],
238        [],
239        [])
240
241    #
242    # Ignored by PGI-6.2.5; -- recognized by output-parser
243    #
244    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([always_inline],
245        [int foo (int arg) __attribute__ ((__always_inline__));],
246        [],
247        [])
248
249    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([cold],
250        [
251         int foo(int arg1, int arg2) __attribute__ ((__cold__));
252         int foo(int arg1, int arg2) { return arg1 * arg2 + arg1; }
253        ],
254        [],
255        [])
256
257    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([const],
258        [
259         int foo(int arg1, int arg2) __attribute__ ((__const__));
260         int foo(int arg1, int arg2) { return arg1 * arg2 + arg1; }
261        ],
262        [],
263        [])
264
265
266    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([deprecated],
267        [
268         int foo(int arg1, int arg2) __attribute__ ((__deprecated__));
269         int foo(int arg1, int arg2) { return arg1 * arg2 + arg1; }
270        ],
271        [],
272        [])
273
274
275    HWLOC_ATTRIBUTE_CFLAGS=
276    case "$hwloc_c_vendor" in
277        gnu)
278            HWLOC_ATTRIBUTE_CFLAGS="-Wall"
279            ;;
280        intel)
281            # we want specifically the warning on format string conversion
282            HWLOC_ATTRIBUTE_CFLAGS="-we181"
283            ;;
284    esac
285    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([format],
286        [
287         int this_printf (void *my_object, const char *my_format, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
288        ],
289        [
290         static int usage (int * argument);
291         extern int this_printf (int arg1, const char *my_format, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
292
293         static int usage (int * argument) {
294             return this_printf (*argument, "%d", argument); /* This should produce a format warning */
295         }
296         /* The autoconf-generated main-function is int main(), which produces a warning by itself */
297         int main(void);
298        ],
299        [$HWLOC_ATTRIBUTE_CFLAGS])
300
301    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([hot],
302        [
303         int foo(int arg1, int arg2) __attribute__ ((__hot__));
304         int foo(int arg1, int arg2) { return arg1 * arg2 + arg1; }
305        ],
306        [],
307        [])
308
309    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([malloc],
310        [
311#ifdef HAVE_STDLIB_H
312#  include <stdlib.h>
313#endif
314         int * foo(int arg1) __attribute__ ((__malloc__));
315         int * foo(int arg1) { return (int*) malloc(arg1); }
316        ],
317        [],
318        [])
319
320
321    #
322    # Attribute may_alias: No suitable cross-check available, that works for non-supporting compilers
323    # Ignored by intel-9.1.045 -- turn off with -wd1292
324    # Ignored by PGI-6.2.5; ignore not detected due to missing cross-check
325    # The test case is chosen to match our only use in topology-xml-*.c, and reproduces an xlc-13.1.0 bug.
326    #
327    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([may_alias],
328        [struct { int i; } __attribute__ ((__may_alias__)) * p_value;],
329        [],
330        [])
331
332
333    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([no_instrument_function],
334        [int * foo(int arg1) __attribute__ ((__no_instrument_function__));],
335        [],
336        [])
337
338
339    #
340    # Attribute nonnull:
341    # Ignored by intel-compiler 9.1.045 -- recognized by cross-check
342    # Ignored by PGI-6.2.5 (pgCC) -- recognized by cross-check
343    #
344    HWLOC_ATTRIBUTE_CFLAGS=
345    case "$hwloc_c_vendor" in
346        gnu)
347            HWLOC_ATTRIBUTE_CFLAGS="-Wall"
348            ;;
349        intel)
350            # we do not want to get ignored attributes warnings, but rather real warnings
351            HWLOC_ATTRIBUTE_CFLAGS="-wd1292"
352            ;;
353    esac
354    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([nonnull],
355        [
356         int square(int *arg) __attribute__ ((__nonnull__));
357         int square(int *arg) { return *arg; }
358        ],
359        [
360         static int usage(int * argument);
361         int square(int * argument) __attribute__ ((__nonnull__));
362         int square(int * argument) { return (*argument) * (*argument); }
363
364         static int usage(int * argument) {
365             return square( ((void*)0) );    /* This should produce an argument must be nonnull warning */
366         }
367         /* The autoconf-generated main-function is int main(), which produces a warning by itself */
368         int main(void);
369        ],
370        [$HWLOC_ATTRIBUTE_CFLAGS])
371
372
373    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([noreturn],
374        [
375#ifdef HAVE_UNISTD_H
376#  include <unistd.h>
377#endif
378#ifdef HAVE_STDLIB_H
379#  include <stdlib.h>
380#endif
381         void fatal(int arg1) __attribute__ ((__noreturn__));
382         void fatal(int arg1) { exit(arg1); }
383        ],
384        [],
385        [])
386
387    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([packed],
388        [
389         struct foo {
390             char a;
391             int x[2] __attribute__ ((__packed__));
392         };
393        ],
394        [],
395        [])
396
397    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([pure],
398        [
399         int square(int arg) __attribute__ ((__pure__));
400         int square(int arg) { return arg * arg; }
401        ],
402        [],
403        [])
404
405    #
406    # Attribute sentinel:
407    # Ignored by the intel-9.1.045 -- recognized by cross-check
408    #                intel-10.0beta works fine
409    # Ignored by PGI-6.2.5 (pgCC) -- recognized by output-parser and cross-check
410    # Ignored by pathcc-2.2.1 -- recognized by cross-check (through grep ignore)
411    #
412    HWLOC_ATTRIBUTE_CFLAGS=
413    case "$hwloc_c_vendor" in
414        gnu)
415            HWLOC_ATTRIBUTE_CFLAGS="-Wall"
416            ;;
417        intel)
418            # we do not want to get ignored attributes warnings
419            HWLOC_ATTRIBUTE_CFLAGS="-wd1292"
420            ;;
421    esac
422    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([sentinel],
423        [
424         int my_execlp(const char * file, const char *arg, ...) __attribute__ ((__sentinel__));
425        ],
426        [
427         static int usage(int * argument);
428         int my_execlp(const char * file, const char *arg, ...) __attribute__ ((__sentinel__));
429
430         static int usage(int * argument) {
431             void * last_arg_should_be_null = argument;
432             return my_execlp ("lala", "/home/there", last_arg_should_be_null);   /* This should produce a warning */
433         }
434         /* The autoconf-generated main-function is int main(), which produces a warning by itself */
435         int main(void);
436        ],
437        [$HWLOC_ATTRIBUTE_CFLAGS])
438
439    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([unused],
440        [
441         int square(int arg1 __attribute__ ((__unused__)), int arg2);
442         int square(int arg1, int arg2) { return arg2; }
443        ],
444        [],
445        [])
446
447
448    #
449    # Attribute warn_unused_result:
450    # Ignored by the intel-compiler 9.1.045 -- recognized by cross-check
451    # Ignored by pathcc-2.2.1 -- recognized by cross-check (through grep ignore)
452    #
453    HWLOC_ATTRIBUTE_CFLAGS=
454    case "$hwloc_c_vendor" in
455        gnu)
456            HWLOC_ATTRIBUTE_CFLAGS="-Wall"
457            ;;
458        intel)
459            # we do not want to get ignored attributes warnings
460            HWLOC_ATTRIBUTE_CFLAGS="-wd1292"
461            ;;
462    esac
463    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([warn_unused_result],
464        [
465         int foo(int arg) __attribute__ ((__warn_unused_result__));
466         int foo(int arg) { return arg + 3; }
467        ],
468        [
469         static int usage(int * argument);
470         int foo(int arg) __attribute__ ((__warn_unused_result__));
471
472         int foo(int arg) { return arg + 3; }
473         static int usage(int * argument) {
474           foo (*argument);        /* Should produce an unused result warning */
475           return 0;
476         }
477
478         /* The autoconf-generated main-function is int main(), which produces a warning by itself */
479         int main(void);
480        ],
481        [$HWLOC_ATTRIBUTE_CFLAGS])
482
483
484    _HWLOC_CHECK_SPECIFIC_ATTRIBUTE([weak_alias],
485        [
486         int foo(int arg);
487         int foo(int arg) { return arg + 3; }
488         int foo2(int arg) __attribute__ ((__weak__, __alias__("foo")));
489        ],
490        [],
491        [])
492
493  fi
494
495  # Now that all the values are set, define them
496
497  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_ALIGNED, [$hwloc_cv___attribute__aligned],
498                     [Whether your compiler has __attribute__ aligned or not])
499  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_ALWAYS_INLINE, [$hwloc_cv___attribute__always_inline],
500                     [Whether your compiler has __attribute__ always_inline or not])
501  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_COLD, [$hwloc_cv___attribute__cold],
502                     [Whether your compiler has __attribute__ cold or not])
503  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_CONST, [$hwloc_cv___attribute__const],
504                     [Whether your compiler has __attribute__ const or not])
505  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_DEPRECATED, [$hwloc_cv___attribute__deprecated],
506                     [Whether your compiler has __attribute__ deprecated or not])
507  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_FORMAT, [$hwloc_cv___attribute__format],
508                     [Whether your compiler has __attribute__ format or not])
509  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_HOT, [$hwloc_cv___attribute__hot],
510                     [Whether your compiler has __attribute__ hot or not])
511  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_MALLOC, [$hwloc_cv___attribute__malloc],
512                     [Whether your compiler has __attribute__ malloc or not])
513  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_MAY_ALIAS, [$hwloc_cv___attribute__may_alias],
514                     [Whether your compiler has __attribute__ may_alias or not])
515  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_NO_INSTRUMENT_FUNCTION, [$hwloc_cv___attribute__no_instrument_function],
516                     [Whether your compiler has __attribute__ no_instrument_function or not])
517  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_NONNULL, [$hwloc_cv___attribute__nonnull],
518                     [Whether your compiler has __attribute__ nonnull or not])
519  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_NORETURN, [$hwloc_cv___attribute__noreturn],
520                     [Whether your compiler has __attribute__ noreturn or not])
521  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_PACKED, [$hwloc_cv___attribute__packed],
522                     [Whether your compiler has __attribute__ packed or not])
523  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_PURE, [$hwloc_cv___attribute__pure],
524                     [Whether your compiler has __attribute__ pure or not])
525  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_SENTINEL, [$hwloc_cv___attribute__sentinel],
526                     [Whether your compiler has __attribute__ sentinel or not])
527  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_UNUSED, [$hwloc_cv___attribute__unused],
528                     [Whether your compiler has __attribute__ unused or not])
529  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_WARN_UNUSED_RESULT, [$hwloc_cv___attribute__warn_unused_result],
530                     [Whether your compiler has __attribute__ warn unused result or not])
531  AC_DEFINE_UNQUOTED(HWLOC_HAVE_ATTRIBUTE_WEAK_ALIAS, [$hwloc_cv___attribute__weak_alias],
532                     [Whether your compiler has __attribute__ weak alias or not])
533])
534
535